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;
}
}
}