Implement unified API controller pattern
This commit is contained in:
@@ -1,54 +0,0 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using AutoMapper;
|
||||
using EstusShots.Server.Models;
|
||||
using EstusShots.Server.Services;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Dto = EstusShots.Shared.Dto;
|
||||
|
||||
namespace EstusShots.Server.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("/api/[controller]")]
|
||||
public class SeasonController : ControllerBase
|
||||
{
|
||||
private readonly EstusShotsContext _context;
|
||||
private readonly IMapper _mapper;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public SeasonController(EstusShotsContext context, IMapper mapper, ILogger<SeasonController> logger)
|
||||
{
|
||||
_context = context;
|
||||
_mapper = mapper;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
public async Task<ActionResult<Dto.Season>> GetSeason(Guid id)
|
||||
{
|
||||
var season = await _context.Seasons.FindAsync(id);
|
||||
if (season == null) {return NotFound();}
|
||||
|
||||
var seasonDto = _mapper.Map<Dto.Season>(season);
|
||||
return seasonDto;
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<Dto.Season>> CreateSeason(Dto.Season season)
|
||||
{
|
||||
var dbSeason = _mapper.Map<Season>(season);
|
||||
_context.Seasons.Add(dbSeason);
|
||||
try
|
||||
{
|
||||
await _context.SaveChangesAsync();
|
||||
_logger.LogInformation("New season created");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e, "Error while saving Season");
|
||||
}
|
||||
return CreatedAtAction(nameof(GetSeason), new {id = dbSeason.SeasonId}, dbSeason);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,33 +1,70 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using AutoMapper;
|
||||
using EstusShots.Server.Services;
|
||||
using EstusShots.Shared.Interfaces;
|
||||
using EstusShots.Shared.Models;
|
||||
using EstusShots.Shared.Models.Parameters;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Dto = EstusShots.Shared.Dto;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace EstusShots.Server.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("/api/[controller]")]
|
||||
public class SeasonsController : ControllerBase
|
||||
[Route("/api/[controller]/[action]")]
|
||||
public class SeasonsController : ControllerBase, ISeasonsController
|
||||
{
|
||||
private readonly EstusShotsContext _context;
|
||||
private readonly IMapper _mapper;
|
||||
private readonly ILogger _logger;
|
||||
private readonly SeasonsService _seasonsService;
|
||||
|
||||
|
||||
public SeasonsController(EstusShotsContext context, IMapper mapper)
|
||||
public SeasonsController(ILogger<SeasonsController> logger, SeasonsService seasonsService)
|
||||
{
|
||||
_context = context;
|
||||
_mapper = mapper;
|
||||
_seasonsService = seasonsService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<List<Dto.Season>>> GetSeasons()
|
||||
[HttpPost]
|
||||
public async Task<ApiResponse<GetSeasonsResponse>> GetSeasons(GetSeasonsParameter parameter)
|
||||
{
|
||||
var seasons = await _context.Seasons.ToListAsync();
|
||||
var dtos = _mapper.Map<List<Dto.Season>>(seasons);
|
||||
return dtos;
|
||||
try
|
||||
{
|
||||
_logger.LogInformation($"Request received from client '{Request.HttpContext.Connection.RemoteIpAddress}'");
|
||||
return await _seasonsService.GetSeasons(parameter);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e, "Exception Occured");
|
||||
return new ApiResponse<GetSeasonsResponse>(new OperationResult(e));
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<ApiResponse<GetSeasonResponse>> GetSeason(GetSeasonParameter parameter)
|
||||
{
|
||||
try
|
||||
{
|
||||
_logger.LogInformation($"Request received from client '{Request.HttpContext.Connection.RemoteIpAddress}'");
|
||||
return await _seasonsService.GetSeason(parameter);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e, "Exception Occured");
|
||||
return new ApiResponse<GetSeasonResponse>(new OperationResult(e));
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ApiResponse<SaveSeasonResponse>> SaveSeason(SaveSeasonParameter parameter)
|
||||
{
|
||||
try
|
||||
{
|
||||
_logger.LogInformation($"Request received from client '{Request.HttpContext.Connection.RemoteIpAddress}'");
|
||||
return await _seasonsService.SaveSeason(parameter);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e, "Exception Occured");
|
||||
return new ApiResponse<SaveSeasonResponse>(new OperationResult(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,23 +1,7 @@
|
||||
{
|
||||
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:26850",
|
||||
"sslPort": 44318
|
||||
}
|
||||
},
|
||||
"profiles": {
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "weatherforecast",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"EstusShots.Server": {
|
||||
"EstusShots.Server (Dev)": {
|
||||
"commandName": "Project",
|
||||
"launchBrowser": false,
|
||||
"launchUrl": "",
|
||||
@@ -25,6 +9,15 @@
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"EstusShots.Server (Prod)": {
|
||||
"commandName": "Project",
|
||||
"launchBrowser": false,
|
||||
"launchUrl": "",
|
||||
"applicationUrl": "https://localhost:5001;http://localhost:5000",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Production"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
63
EstusShots.Server/Services/SeasonsService.cs
Normal file
63
EstusShots.Server/Services/SeasonsService.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using AutoMapper;
|
||||
using EstusShots.Server.Models;
|
||||
using EstusShots.Shared.Interfaces;
|
||||
using EstusShots.Shared.Models;
|
||||
using EstusShots.Shared.Models.Parameters;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Dto = EstusShots.Shared.Dto;
|
||||
|
||||
namespace EstusShots.Server.Services
|
||||
{
|
||||
public class SeasonsService : ISeasonsController
|
||||
{
|
||||
private readonly EstusShotsContext _context;
|
||||
private readonly IMapper _mapper;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public SeasonsService(EstusShotsContext context, IMapper mapper, ILogger<SeasonsService> logger)
|
||||
{
|
||||
_context = context;
|
||||
_mapper = mapper;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<ApiResponse<GetSeasonsResponse>> GetSeasons(GetSeasonsParameter parameter)
|
||||
{
|
||||
var seasons = await _context.Seasons.ToListAsync();
|
||||
var dtos = _mapper.Map<List<Dto.Season>>(seasons);
|
||||
return new ApiResponse<GetSeasonsResponse>(new GetSeasonsResponse(dtos));
|
||||
}
|
||||
|
||||
public async Task<ApiResponse<GetSeasonResponse>> GetSeason(GetSeasonParameter parameter)
|
||||
{
|
||||
var season = await _context.Seasons.FindAsync(parameter.SeasonId);
|
||||
if (season == null)
|
||||
{
|
||||
_logger.LogWarning($"Season '{parameter.SeasonId}' not found in database");
|
||||
return new ApiResponse<GetSeasonResponse>(new OperationResult(false, $"Season '{parameter.SeasonId}' not found in database"));
|
||||
}
|
||||
var seasonDto = _mapper.Map<Dto.Season>(season);
|
||||
return new ApiResponse<GetSeasonResponse>(new GetSeasonResponse(seasonDto));
|
||||
}
|
||||
|
||||
public async Task<ApiResponse<SaveSeasonResponse>> SaveSeason(SaveSeasonParameter parameter)
|
||||
{
|
||||
var season = _mapper.Map<Season>(parameter.Season);
|
||||
var existing = await _context.Seasons.FindAsync(season.SeasonId);
|
||||
if (existing == null)
|
||||
{
|
||||
_context.Seasons.Add(season);await _context.SaveChangesAsync();
|
||||
_logger.LogInformation($"New season created: '{season.SeasonId}'");
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
return new ApiResponse<SaveSeasonResponse>(new SaveSeasonResponse(season.SeasonId));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,37 +6,46 @@ using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using IHostingEnvironment = Microsoft.AspNetCore.Hosting.IHostingEnvironment;
|
||||
|
||||
namespace EstusShots.Server
|
||||
{
|
||||
public class Startup
|
||||
{
|
||||
public IConfiguration Configuration { get; }
|
||||
|
||||
private bool IsDevelopment { get; set; }
|
||||
|
||||
public Startup(IConfiguration configuration)
|
||||
{
|
||||
Configuration = configuration;
|
||||
}
|
||||
|
||||
public IConfiguration Configuration { get; }
|
||||
|
||||
// This method gets called by the runtime. Use this method to add services to the container.
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddDbContext<EstusShotsContext>(
|
||||
opt => opt.UseInMemoryDatabase("debug.db"));
|
||||
services.AddAutoMapper(typeof(Startup));
|
||||
services.AddControllers();
|
||||
services.AddControllers().AddJsonOptions(options =>
|
||||
{
|
||||
if (IsDevelopment)
|
||||
{
|
||||
options.JsonSerializerOptions.WriteIndented = true;
|
||||
}
|
||||
});
|
||||
services.AddScoped<SeasonsService>();
|
||||
}
|
||||
|
||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||
{
|
||||
if (env.IsDevelopment())
|
||||
IsDevelopment = env.IsDevelopment();
|
||||
if (IsDevelopment)
|
||||
{
|
||||
app.UseDeveloperExceptionPage();
|
||||
}
|
||||
|
||||
if (!env.IsDevelopment())
|
||||
else
|
||||
{
|
||||
app.UseHttpsRedirection();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user