diff --git a/LaDOSE.Src/LaDOSE.Api/Controllers/GameController.cs b/LaDOSE.Src/LaDOSE.Api/Controllers/GameController.cs index 259eb36..8321a2d 100644 --- a/LaDOSE.Src/LaDOSE.Api/Controllers/GameController.cs +++ b/LaDOSE.Src/LaDOSE.Api/Controllers/GameController.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using LaDOSE.Business.Interface; using LaDOSE.Entity; using LaDOSE.Entity.Context; using Microsoft.AspNetCore.Authorization; @@ -15,28 +16,33 @@ namespace LaDOSE.Api.Controllers public class GameController : ControllerBase { - private readonly LaDOSEDbContext _db; + private readonly IGameService _gameService; - public GameController(LaDOSEDbContext db) + public GameController(IGameService gameService) { - _db = db; + _gameService = gameService; } - // GET api/Config + // GET api/Game [HttpGet] public List Get() { - - return _db.Game.ToList(); - + + return _gameService.GetAll().ToList(); + } - // GET api/Config/5 + // GET api/Game/5 [HttpGet("{id}")] public Game Get(int id) { - return _db.Game.FirstOrDefault(e=>e.Id==id); + return _gameService.GetById(id); } - - + + [HttpPut()] + public bool Put(Game game) + { + return _gameService.Update(game); + } + } } diff --git a/LaDOSE.Src/LaDOSE.Api/Startup.cs b/LaDOSE.Src/LaDOSE.Api/Startup.cs index a10e57d..d645199 100644 --- a/LaDOSE.Src/LaDOSE.Api/Startup.cs +++ b/LaDOSE.Src/LaDOSE.Api/Startup.cs @@ -87,6 +87,7 @@ namespace LaDOSE.Api // configure DI for application services services.AddScoped(); + services.AddScoped(); } diff --git a/LaDOSE.Src/LaDOSE.Entity/Context/LaDOSEDbContext.cs b/LaDOSE.Src/LaDOSE.Entity/Context/LaDOSEDbContext.cs index 17b0d6f..61bf22a 100644 --- a/LaDOSE.Src/LaDOSE.Entity/Context/LaDOSEDbContext.cs +++ b/LaDOSE.Src/LaDOSE.Entity/Context/LaDOSEDbContext.cs @@ -7,6 +7,7 @@ namespace LaDOSE.Entity.Context public DbSet Game { get; set; } public DbSet ApplicationUser { get; set; } public DbSet Season { get; set; } + //public DbSet SeasonGame { get; set; } public LaDOSEDbContext(DbContextOptions options) : base(options) { @@ -14,7 +15,24 @@ namespace LaDOSE.Entity.Context protected override void OnModelCreating(ModelBuilder modelBuilder) { + + base.OnModelCreating(modelBuilder); + modelBuilder.Entity() + .HasKey(t => new { t.SeasonId, t.GameId }); + + + + modelBuilder.Entity() + .HasOne(pt => pt.Season) + .WithMany(p => p.Games) + .HasForeignKey(pt => pt.GameId); + + modelBuilder.Entity() + .HasOne(pt => pt.Game) + .WithMany(p => p.Seasons) + .HasForeignKey(pt => pt.SeasonId); + } } diff --git a/LaDOSE.Src/LaDOSE.Entity/Game.cs b/LaDOSE.Src/LaDOSE.Entity/Game.cs index d3f0021..c20751b 100644 --- a/LaDOSE.Src/LaDOSE.Entity/Game.cs +++ b/LaDOSE.Src/LaDOSE.Entity/Game.cs @@ -1,11 +1,16 @@ using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; namespace LaDOSE.Entity { public class Game { + [Key] public int Id { get; set; } public string Name { get; set; } public string ImgUrl { get; set; } + + public virtual IEnumerable Seasons { get; set; } } } diff --git a/LaDOSE.Src/LaDOSE.Entity/Season.cs b/LaDOSE.Src/LaDOSE.Entity/Season.cs index 6604359..8589db6 100644 --- a/LaDOSE.Src/LaDOSE.Entity/Season.cs +++ b/LaDOSE.Src/LaDOSE.Entity/Season.cs @@ -1,15 +1,18 @@ using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; namespace LaDOSE.Entity { public class Season { + [Key] public int Id { get; set; } public string Name { get; set; } public DateTime StartDate { get; set; } public DateTime EndDate { get; set; } - + public virtual IEnumerable Games { get; set; } } } \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Entity/SeasonGame.cs b/LaDOSE.Src/LaDOSE.Entity/SeasonGame.cs new file mode 100644 index 0000000..9b42d36 --- /dev/null +++ b/LaDOSE.Src/LaDOSE.Entity/SeasonGame.cs @@ -0,0 +1,11 @@ +namespace LaDOSE.Entity +{ + public class SeasonGame + { + public int GameId { get; set; } + public Game Game { get; set; } + public int SeasonId { get; set; } + public Season Season { get; set; } + + } +} \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Service/Interface/IGameService.cs b/LaDOSE.Src/LaDOSE.Service/Interface/IGameService.cs new file mode 100644 index 0000000..c27c752 --- /dev/null +++ b/LaDOSE.Src/LaDOSE.Service/Interface/IGameService.cs @@ -0,0 +1,15 @@ +using System.Collections.Generic; +using LaDOSE.Entity; + +namespace LaDOSE.Business.Interface +{ + public interface IGameService + { + + IEnumerable GetAll(); + Game GetById(int id); + Game Create(Game game); + bool Update(Game game); + void Delete(int id); + } +} \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Service/Service/GameService.cs b/LaDOSE.Src/LaDOSE.Service/Service/GameService.cs new file mode 100644 index 0000000..b08b16e --- /dev/null +++ b/LaDOSE.Src/LaDOSE.Service/Service/GameService.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using LaDOSE.Business.Interface; +using LaDOSE.Entity; +using LaDOSE.Entity.Context; +using Microsoft.EntityFrameworkCore; + +namespace LaDOSE.Business.Service +{ + public class GameService : IGameService + { + private LaDOSEDbContext _context; + + public GameService(LaDOSEDbContext context) + { + _context = context; + } + + public IEnumerable GetAll() + { + return _context.Game.Include(e => e.Seasons).ToList(); + } + + public Game GetById(int id) + { + return _context.Game.Include(e => e.Seasons).FirstOrDefault(e=>e.Id == id); + } + + public Game Create(Game game) + { + if (game.Id != 0) + { + throw new Exception("Id is invalid"); + } + var gameAdded = _context.Game.Add(game); + _context.SaveChanges(); + return gameAdded.Entity; + } + + public bool Update(Game game) + { + if (game.Id == 0) + { + throw new Exception("Id is invalid"); + } + + var gameUpdated = _context.Game.FirstOrDefault(e => e == game); + + gameUpdated = game; + try + { + _context.SaveChanges(); + return true; + } + catch (Exception e) + { + Console.WriteLine(e); + } + + return false; + + + } + + public void Delete(int id) + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file