Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create new component (Movie) & fix SonarCloud issues (#2)
* Fix issues raised by SonarCloud * Create Api with CarHistory CRUD * Activate authentication with Firebase * Add MovieComponent * Delete .csproj.user and add it in gitignore * Fix SonarCloud issues
- Loading branch information
Showing
42 changed files
with
1,350 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,5 @@ | |
Debug/ | ||
Release/ | ||
obj/ | ||
appsettings.Development.json | ||
*.csproj.user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Keep track .NET solution | ||
|
||
[![Build Status](https://dev.azure.com/devprofr/open-source/_apis/build/status/keeptrack-CI?branchName=master)](https://dev.azure.com/devprofr/open-source/_build/latest?definitionId=18&branchName=master) | ||
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=devpro.keep-track&metric=alert_status)](https://sonarcloud.io/dashboard?id=devpro.keep-track) | ||
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=devpro.keep-track&metric=coverage)](https://sonarcloud.io/dashboard?id=devpro.keep-track) | ||
|
||
## Dependencies | ||
|
||
- SDK: .NET Core 3.0 | ||
- DB: MongoDB 4.2 | ||
|
||
## Configuration | ||
|
||
- Value for key `KeepTrack_MongoDbConnectionString`: .NET connection string to access MongoDB cluster, ideally set as an environment variable. | ||
|
||
## Local run | ||
|
||
- Clone the solution: `git clone ...` | ||
- Build the solution: `dotnet build` | ||
- Run the console: `dotnet dotnet src\ConsoleApp\bin\Debug\netcoreapp3.0\KeepTrack.ConsoleApp.dll ...` | ||
- Run the web api: `dotnet run --project src\Api` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.0</TargetFramework> | ||
<AssemblyName>KeepTrack.Api</AssemblyName> | ||
<RootNamespace>KeepTrack.Api</RootNamespace> | ||
<ProjectGuid>{651432D5-8481-4807-A422-552EEB4DE8C2}</ProjectGuid> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> | ||
<DebugType>full</DebugType> | ||
<DebugSymbols>true</DebugSymbols> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.Authentication.Google" Version="3.0.0" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.0.0" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="3.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Identity.Core" Version="3.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.0.0" /> | ||
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.0.0" /> | ||
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0-rc4" /> | ||
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="5.0.0-rc4" /> | ||
<PackageReference Include="Withywoods.Configuration" Version="1.1.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\CarComponent.Domain\CarComponent.Domain.csproj" /> | ||
<ProjectReference Include="..\CarComponent.Infrastructure.MongoDb\CarComponent.Infrastructure.MongoDb.csproj" /> | ||
<ProjectReference Include="..\MovieComponent.Domain\MovieComponent.Domain.csproj" /> | ||
<ProjectReference Include="..\MovieComponent.Infrastructure.MongoDb\MovieComponent.Infrastructure.MongoDb.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using System.Collections.Generic; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.OpenApi.Models; | ||
using Withywoods.Configuration; | ||
using Withywoods.Dal.MongoDb; | ||
using Withywoods.Dal.MongoDb.Serialization; | ||
|
||
namespace KeepTrack.Api | ||
{ | ||
/// <summary> | ||
/// Web application configuration. | ||
/// This class implements the interface from the libraries that are used in the application. | ||
/// </summary> | ||
public class AppConfiguration : IMongoDbConfiguration | ||
{ | ||
#region Constructor & private fields | ||
|
||
/// <summary> | ||
/// Create a new instance of <see cref="AppConfiguration"/> | ||
/// </summary> | ||
/// <param name="configurationRoot"></param> | ||
public AppConfiguration(IConfiguration configurationRoot) | ||
{ | ||
ConfigurationRoot = configurationRoot; | ||
} | ||
|
||
/// <summary> | ||
/// Configuration root. | ||
/// </summary> | ||
public IConfiguration ConfigurationRoot { get; set; } | ||
|
||
#endregion | ||
|
||
#region IMongoDbConfiguration properties | ||
|
||
/// <summary> | ||
/// MongoDB connection string => secret! | ||
/// This is really a sensitive information so better defined as an environment variable. | ||
/// </summary> | ||
public string ConnectionString => ConfigurationRoot.TryGetSection("KeepTrack_MongoDbConnectionString").Value; | ||
|
||
/// <summary> | ||
/// MongoDB collection name. | ||
/// </summary> | ||
public string DatabaseName => ConfigurationRoot.TryGetSection("Infrastructure:MongoDB:DatabaseName").Value; | ||
|
||
/// <summary> | ||
/// MongoDB serialization conventions. | ||
/// </summary> | ||
public List<string> SerializationConventions => | ||
new List<string> | ||
{ | ||
ConventionValues.CamelCaseElementName, | ||
ConventionValues.EnumAsString, | ||
ConventionValues.IgnoreExtraElements, | ||
ConventionValues.IgnoreNullValues | ||
}; | ||
|
||
#endregion | ||
|
||
#region General properties | ||
|
||
/// <summary> | ||
/// Open API information. | ||
/// </summary> | ||
public OpenApiInfo OpenApiInfo => | ||
new OpenApiInfo | ||
{ | ||
Title = "Keep Track API", | ||
Version = "1.0" | ||
}; | ||
|
||
#endregion | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using AutoMapper; | ||
using KeepTrack.Api.Dto; | ||
using KeepTrack.CarComponent.Domain; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace Api.Controllers | ||
{ | ||
/// <summary> | ||
/// Car history controller. | ||
/// </summary> | ||
[ApiController] | ||
[Authorize] | ||
[Route("api/car-history")] | ||
public class CarHistoryController : KeepTrack.Api.Controllers.ControllerBase | ||
{ | ||
private readonly IMapper _mapper; | ||
private readonly ICarHistoryRepository _carHistoryRepository; | ||
|
||
/// <summary> | ||
/// Creates a new instance of <see cref="CarHistoryController"/>. | ||
/// </summary> | ||
/// <param name="mapper"></param> | ||
/// <param name="carHistoryRepository"></param> | ||
public CarHistoryController(IMapper mapper, ICarHistoryRepository carHistoryRepository) | ||
{ | ||
_mapper = mapper; | ||
_carHistoryRepository = carHistoryRepository; | ||
} | ||
|
||
/// <summary> | ||
/// Gets all the history for a given car. | ||
/// </summary> | ||
/// <param name="carId">Car ID</param> | ||
/// <returns></returns> | ||
[HttpGet] | ||
[ProducesResponseType(200, Type = typeof(List<CarHistoryDto>))] | ||
[ProducesResponseType(400)] | ||
[ProducesResponseType(500)] | ||
public async Task<IActionResult> Get(string carId) | ||
{ | ||
if (string.IsNullOrEmpty(carId)) | ||
{ | ||
return BadRequest(); | ||
} | ||
|
||
var models = await _carHistoryRepository.FindAllAsync(carId, GetUserId()); | ||
return Ok(_mapper.Map<List<CarHistoryDto>>(models)); | ||
} | ||
|
||
/// <summary> | ||
/// Gets information from a single car history. | ||
/// </summary> | ||
/// <param name="id"></param> | ||
/// <returns></returns> | ||
[HttpGet("{id}", Name = "GetCarHistoryById")] | ||
[ProducesResponseType(200, Type = typeof(CarHistoryDto))] | ||
[ProducesResponseType(400)] | ||
[ProducesResponseType(404)] | ||
[ProducesResponseType(500)] | ||
public async Task<IActionResult> GetById(string id) | ||
{ | ||
if (string.IsNullOrEmpty(id)) | ||
{ | ||
return BadRequest(); | ||
} | ||
|
||
var model = await _carHistoryRepository.FindOneAsync(id, GetUserId()); | ||
if (model == null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
return Ok(_mapper.Map<CarHistoryDto>(model)); | ||
} | ||
|
||
/// <summary> | ||
/// Creates a new car history. | ||
/// </summary> | ||
/// <param name="dto"></param> | ||
[HttpPost] | ||
[ProducesResponseType(201)] | ||
public async Task<IActionResult> Post([FromBody] CarHistoryDto dto) | ||
{ | ||
var input = _mapper.Map<CarHistoryModel>(dto); | ||
input.OwnerId = GetUserId(); | ||
var model = await _carHistoryRepository.CreateAsync(input); | ||
return CreatedAtRoute("GetCarHistoryById", new { id = model.Id }, _mapper.Map<CarHistoryDto>(model)); | ||
} | ||
|
||
/// <summary> | ||
/// Updates a car history. | ||
/// </summary> | ||
/// <param name="id"></param> | ||
/// <param name="dto"></param> | ||
[HttpPut("{id}")] | ||
[ProducesResponseType(204)] | ||
[ProducesResponseType(400)] | ||
[ProducesResponseType(500)] | ||
public async Task<IActionResult> Put(string id, [FromBody] CarHistoryDto dto) | ||
{ | ||
if (string.IsNullOrEmpty(id)) | ||
{ | ||
return BadRequest(); | ||
} | ||
|
||
var input = _mapper.Map<CarHistoryModel>(dto); | ||
input.OwnerId = GetUserId(); | ||
await _carHistoryRepository.UpdateAsync(id, input, GetUserId()); | ||
return NoContent(); | ||
} | ||
|
||
/// <summary> | ||
/// Deletes a car history. | ||
/// </summary> | ||
/// <param name="id"></param> | ||
/// <returns></returns> | ||
[HttpDelete("{id}")] | ||
[ProducesResponseType(204)] | ||
[ProducesResponseType(400)] | ||
[ProducesResponseType(500)] | ||
public async Task<IActionResult> Delete(string id) | ||
{ | ||
if (string.IsNullOrEmpty(id)) | ||
{ | ||
return BadRequest(); | ||
} | ||
|
||
await _carHistoryRepository.DeleteAsync(id, GetUserId()); | ||
return NoContent(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
using System.Linq; | ||
|
||
namespace KeepTrack.Api.Controllers | ||
{ | ||
/// <summary> | ||
/// Base controller for the web application. | ||
/// </summary> | ||
public abstract class ControllerBase : Microsoft.AspNetCore.Mvc.ControllerBase | ||
{ | ||
/// <summary> | ||
/// Get authenticated user id. | ||
/// </summary> | ||
/// <returns></returns> | ||
protected string GetUserId() | ||
{ | ||
var userId = User.Claims.FirstOrDefault(x => x.Type == "user_id")?.Value; | ||
if (string.IsNullOrEmpty(userId)) | ||
{ | ||
throw new UnauthorizedAccessException(); | ||
} | ||
|
||
return userId; | ||
} | ||
} | ||
} |
Oops, something went wrong.