This commit is contained in:
2020-02-26 21:50:58 +01:00
parent 78c21ade79
commit 64cdaf8e9f
15 changed files with 353 additions and 66 deletions

View File

@@ -0,0 +1,43 @@
using System;
using System.Threading.Tasks;
using AutoMapper;
using EstusShots.Server.Models;
using EstusShots.Server.Services;
using Microsoft.AspNetCore.Mvc;
using Dto = EstusShots.Shared.Models;
namespace EstusShots.Server.Controllers
{
[ApiController]
[Route("/api/[controller]")]
public class SeasonController : ControllerBase
{
private readonly EstusShotsContext _context;
private readonly IMapper _mapper;
public SeasonController(EstusShotsContext context, IMapper mapper)
{
_context = context;
_mapper = mapper;
}
[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);
await _context.SaveChangesAsync();
return CreatedAtAction(nameof(GetSeason), new {id = dbSeason.SeasonId}, dbSeason);
}
}
}

View File

@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using AutoMapper;
using EstusShots.Server.Services;
using EstusShots.Shared.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Dto = EstusShots.Shared.Models;
namespace EstusShots.Server.Controllers
{
@@ -11,26 +13,21 @@ namespace EstusShots.Server.Controllers
public class SeasonsController : ControllerBase
{
private readonly EstusShotsContext _context;
private readonly IMapper _mapper;
public SeasonsController(EstusShotsContext context)
public SeasonsController(EstusShotsContext context, IMapper mapper)
{
_context = context;
}
[HttpGet("{id}")]
public async Task<ActionResult<Season>> GetSeason(Guid id)
{
var season = await _context.Seasons.FindAsync(id);
if (season == null) return NotFound();
return season;
_mapper = mapper;
}
[HttpPost]
public async Task<ActionResult<Season>> CreateSeason(Season season)
[HttpGet]
public async Task<ActionResult<List<Dto.Season>>> GetSeasons()
{
_context.Seasons.Add(season);
await _context.SaveChangesAsync();
return CreatedAtAction(nameof(GetSeason), new {id = season.SeasonId}, season);
var seasons = await _context.Seasons.ToListAsync();
var dtos = _mapper.Map<List<Dto.Season>>(seasons);
return dtos;
}
}
}

View File

@@ -14,6 +14,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoMapper" Version="9.0.0" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.2" />

View File

@@ -0,0 +1,14 @@
using AutoMapper;
using EstusShots.Server.Models;
namespace EstusShots.Server.Mapping
{
public class Profiles : Profile
{
public Profiles()
{
CreateMap<Season, Shared.Models.Season>();
CreateMap<Shared.Models.Season, Season>();
}
}
}

View File

@@ -0,0 +1,20 @@
using System;
using System.ComponentModel.DataAnnotations;
namespace EstusShots.Server.Models
{
public class Season
{
public Guid SeasonId { get; set; }
public int Number { get; set; }
[MaxLength(50)] public string Game { get; set; } = default!;
public string? Description { get; set; }
public DateTime Start { get; set; }
public DateTime? End { get; set; }
}
}

View File

@@ -1,4 +1,4 @@
using EstusShots.Shared.Models;
using EstusShots.Server.Models;
using Microsoft.EntityFrameworkCore;
namespace EstusShots.Server.Services

View File

@@ -1,3 +1,4 @@
using AutoMapper;
using EstusShots.Server.Services;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
@@ -22,6 +23,7 @@ namespace EstusShots.Server
{
services.AddDbContext<EstusShotsContext>(
opt => opt.UseInMemoryDatabase("debug.db"));
services.AddAutoMapper(typeof(Startup));
services.AddControllers();
}