Improved client handling and episode models
This commit is contained in:
52
EstusShots.Server/Controllers/EpisodeController.cs
Normal file
52
EstusShots.Server/Controllers/EpisodeController.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using AutoMapper;
|
||||
using EstusShots.Server.Models;
|
||||
using EstusShots.Server.Services;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace EstusShots.Server.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("/api/[controller]")]
|
||||
public class EpisodeController : ControllerBase
|
||||
{
|
||||
private readonly EstusShotsContext _context;
|
||||
private readonly IMapper _mapper;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public EpisodeController(EstusShotsContext context, IMapper mapper, ILogger<EpisodeController> logger)
|
||||
{
|
||||
_context = context;
|
||||
_mapper = mapper;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
public async Task<ActionResult<Shared.Dto.Episode>> GetEpisode(Guid id)
|
||||
{
|
||||
var episode = await _context.Seasons.FindAsync(id);
|
||||
if (episode == null) {return NotFound();}
|
||||
|
||||
var episodeDto = _mapper.Map<Shared.Dto.Episode>(episode);
|
||||
return episodeDto;
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<Shared.Dto.Episode>> CreateSeason(Shared.Dto.Episode episodeDto)
|
||||
{
|
||||
var episode = _mapper.Map<Episode>(episodeDto);
|
||||
_context.Episodes.Add(episode);
|
||||
try
|
||||
{
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e, "Error while saving object");
|
||||
}
|
||||
return CreatedAtAction(nameof(GetEpisode), new {id = episode.EpisodeId}, episode);
|
||||
}
|
||||
}
|
||||
}
|
||||
38
EstusShots.Server/Controllers/EpisodesController.cs
Normal file
38
EstusShots.Server/Controllers/EpisodesController.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using AutoMapper;
|
||||
using EstusShots.Server.Services;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Dto = EstusShots.Shared.Dto;
|
||||
|
||||
namespace EstusShots.Server.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("/api/[controller]")]
|
||||
public class EpisodesController : ControllerBase
|
||||
{
|
||||
private readonly EstusShotsContext _context;
|
||||
private readonly IMapper _mapper;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public EpisodesController(ILogger<EpisodesController> logger, IMapper mapper, EstusShotsContext context)
|
||||
{
|
||||
_logger = logger;
|
||||
_mapper = mapper;
|
||||
_context = context;
|
||||
}
|
||||
|
||||
[HttpGet("seasonId")]
|
||||
public async Task<ActionResult<List<Dto.Episode>>> GetEpisodes(Guid seasonId)
|
||||
{
|
||||
_logger.LogDebug($"All");
|
||||
var episodes = await _context.Episodes.Where(x => x.SeasonId == seasonId).ToListAsync();
|
||||
var dtos = _mapper.Map<List<Dto.Episode>>(episodes);
|
||||
return dtos;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,8 @@ using AutoMapper;
|
||||
using EstusShots.Server.Models;
|
||||
using EstusShots.Server.Services;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Dto = EstusShots.Shared.Models;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Dto = EstusShots.Shared.Dto;
|
||||
|
||||
namespace EstusShots.Server.Controllers
|
||||
{
|
||||
@@ -14,18 +15,20 @@ namespace EstusShots.Server.Controllers
|
||||
{
|
||||
private readonly EstusShotsContext _context;
|
||||
private readonly IMapper _mapper;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public SeasonController(EstusShotsContext context, IMapper mapper)
|
||||
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();
|
||||
if (season == null) {return NotFound();}
|
||||
|
||||
var seasonDto = _mapper.Map<Dto.Season>(season);
|
||||
return seasonDto;
|
||||
@@ -36,7 +39,15 @@ namespace EstusShots.Server.Controllers
|
||||
{
|
||||
var dbSeason = _mapper.Map<Season>(season);
|
||||
_context.Seasons.Add(dbSeason);
|
||||
await _context.SaveChangesAsync();
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ using AutoMapper;
|
||||
using EstusShots.Server.Services;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Dto = EstusShots.Shared.Models;
|
||||
using Dto = EstusShots.Shared.Dto;
|
||||
|
||||
namespace EstusShots.Server.Controllers
|
||||
{
|
||||
|
||||
@@ -7,8 +7,11 @@ namespace EstusShots.Server.Mapping
|
||||
{
|
||||
public Profiles()
|
||||
{
|
||||
CreateMap<Season, Shared.Models.Season>();
|
||||
CreateMap<Shared.Models.Season, Season>();
|
||||
CreateMap<Season, Shared.Dto.Season>();
|
||||
CreateMap<Shared.Dto.Season, Season>();
|
||||
|
||||
CreateMap<Episode, Shared.Dto.Episode>();
|
||||
CreateMap<Shared.Dto.Episode, Episode>();
|
||||
}
|
||||
}
|
||||
}
|
||||
24
EstusShots.Server/Models/Episode.cs
Normal file
24
EstusShots.Server/Models/Episode.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace EstusShots.Server.Models
|
||||
{
|
||||
public class Episode
|
||||
{
|
||||
public Guid EpisodeId { get; set; }
|
||||
|
||||
public int Number { get; set; }
|
||||
|
||||
[MaxLength(50)] public string Title { get; set; } = default!;
|
||||
|
||||
public DateTime DateTime { get; set; }
|
||||
|
||||
public DateTime Start { get; set; }
|
||||
|
||||
public DateTime? End { get; set; }
|
||||
|
||||
public Guid SeasonId { get; set; }
|
||||
|
||||
public Season Season { get; set; } = default!;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace EstusShots.Server.Models
|
||||
@@ -16,5 +17,7 @@ namespace EstusShots.Server.Models
|
||||
public DateTime Start { get; set; }
|
||||
|
||||
public DateTime? End { get; set; }
|
||||
|
||||
public List<Episode> Episodes { get; set; } = default!;
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,7 @@ namespace EstusShots.Server
|
||||
|
||||
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
||||
Host.CreateDefaultBuilder(args)
|
||||
.ConfigureLogging(builder => { builder.AddConsole(); })
|
||||
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
|
||||
}
|
||||
}
|
||||
@@ -7,8 +7,10 @@ namespace EstusShots.Server.Services
|
||||
{
|
||||
public EstusShotsContext(DbContextOptions options) : base(options)
|
||||
{
|
||||
ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
|
||||
}
|
||||
|
||||
public DbSet<Season> Seasons { get; set; } = default!;
|
||||
public DbSet<Episode> Episodes { get; set; } = default!;
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace EstusShots.Server
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user