From 9a9d4c7053b68408d0ab2a69a78dd109cb1fb410 Mon Sep 17 00:00:00 2001 From: Darkstack <1835601+darkstack@users.noreply.github.com> Date: Sun, 7 Oct 2018 03:03:38 +0200 Subject: [PATCH] Generic Service JSON Reference loop Events --- LaDOSE.Src/LaDOSE.Api/Program.cs | 2 +- LaDOSE.Src/LaDOSE.Api/Startup.cs | 2 +- .../LaDOSE.Entity/Context/LaDOSEDbContext.cs | 6 +- LaDOSE.Src/LaDOSE.Entity/Event.cs | 17 ++ LaDOSE.Src/LaDOSE.Entity/Season.cs | 2 + .../LaDOSE.Service/Interface/IBaseService.cs | 13 ++ .../LaDOSE.Service/Interface/IEventService.cs | 9 + .../LaDOSE.Service/Interface/IGameService.cs | 7 +- .../LaDOSE.Service/Service/BaseService.cs | 44 ++++ .../LaDOSE.Service/Service/EventService.cs | 26 +++ .../LaDOSE.Service/Service/GameService.cs | 52 +---- Sql/configure.sql | 207 +++++++++++++----- 12 files changed, 270 insertions(+), 117 deletions(-) create mode 100644 LaDOSE.Src/LaDOSE.Entity/Event.cs create mode 100644 LaDOSE.Src/LaDOSE.Service/Interface/IBaseService.cs create mode 100644 LaDOSE.Src/LaDOSE.Service/Interface/IEventService.cs create mode 100644 LaDOSE.Src/LaDOSE.Service/Service/BaseService.cs create mode 100644 LaDOSE.Src/LaDOSE.Service/Service/EventService.cs diff --git a/LaDOSE.Src/LaDOSE.Api/Program.cs b/LaDOSE.Src/LaDOSE.Api/Program.cs index b28033f..fb75431 100644 --- a/LaDOSE.Src/LaDOSE.Api/Program.cs +++ b/LaDOSE.Src/LaDOSE.Api/Program.cs @@ -7,9 +7,9 @@ using System.Security.Cryptography.X509Certificates; using System.Threading.Tasks; using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Hosting.Internal; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; - namespace LaDOSE.Api { public class Program diff --git a/LaDOSE.Src/LaDOSE.Api/Startup.cs b/LaDOSE.Src/LaDOSE.Api/Startup.cs index d645199..d8c889b 100644 --- a/LaDOSE.Src/LaDOSE.Api/Startup.cs +++ b/LaDOSE.Src/LaDOSE.Api/Startup.cs @@ -41,7 +41,7 @@ namespace LaDOSE.Api var MySqlUser = this.Configuration["MySql:User"]; var MySqlPassword = this.Configuration["MySql:Password"]; services.AddCors(); - services.AddMvc(); + services.AddMvc().AddJsonOptions(x => x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore); services.AddDbContextPool( // replace "YourDbContext" with the class name of your DbContext options => options.UseMySql($"Server={MySqlServer};Database={MySqlDatabase};User={MySqlUser};Password={MySqlPassword};", // replace with your Connection String mysqlOptions => diff --git a/LaDOSE.Src/LaDOSE.Entity/Context/LaDOSEDbContext.cs b/LaDOSE.Src/LaDOSE.Entity/Context/LaDOSEDbContext.cs index 483e493..ed00c13 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 Event { get; set; } //public DbSet SeasonGame { get; set; } public LaDOSEDbContext(DbContextOptions options) : base(options) @@ -21,7 +22,10 @@ namespace LaDOSE.Entity.Context modelBuilder.Entity() .HasKey(t => new { t.SeasonId, t.GameId }); - + modelBuilder.Entity() + .HasOne(s => s.Season) + .WithMany(p => p.Event) + .HasForeignKey(fk => fk.SeasonId); modelBuilder.Entity() .HasOne(pt => pt.Season) diff --git a/LaDOSE.Src/LaDOSE.Entity/Event.cs b/LaDOSE.Src/LaDOSE.Entity/Event.cs new file mode 100644 index 0000000..2201f5a --- /dev/null +++ b/LaDOSE.Src/LaDOSE.Entity/Event.cs @@ -0,0 +1,17 @@ +using System; +using System.ComponentModel.DataAnnotations; + +namespace LaDOSE.Entity +{ + public class Event + { + [Key] + public int Id { get; set; } + public string Name { get; set; } + public DateTime Date { get; set; } + public int SeasonId { get; set; } + public Season Season { get; set; } + public bool Ranking { get; set; } + + } +} \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Entity/Season.cs b/LaDOSE.Src/LaDOSE.Entity/Season.cs index 8589db6..551bea1 100644 --- a/LaDOSE.Src/LaDOSE.Entity/Season.cs +++ b/LaDOSE.Src/LaDOSE.Entity/Season.cs @@ -14,5 +14,7 @@ namespace LaDOSE.Entity public DateTime EndDate { get; set; } public virtual IEnumerable Games { get; set; } + + public virtual IEnumerable Event { get; set; } } } \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Service/Interface/IBaseService.cs b/LaDOSE.Src/LaDOSE.Service/Interface/IBaseService.cs new file mode 100644 index 0000000..fd06912 --- /dev/null +++ b/LaDOSE.Src/LaDOSE.Service/Interface/IBaseService.cs @@ -0,0 +1,13 @@ +using System.Collections.Generic; + +namespace LaDOSE.Business.Interface +{ + public interface IBaseService where T : class + { + IEnumerable GetAll(); + T GetById(int id); + T Create(T entity); + bool Update(T entity); + bool Delete(int id); + } +} \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Service/Interface/IEventService.cs b/LaDOSE.Src/LaDOSE.Service/Interface/IEventService.cs new file mode 100644 index 0000000..cdc762d --- /dev/null +++ b/LaDOSE.Src/LaDOSE.Service/Interface/IEventService.cs @@ -0,0 +1,9 @@ +using LaDOSE.Entity; + +namespace LaDOSE.Business.Interface +{ + public interface IEventService : IBaseService + { + + } +} \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Service/Interface/IGameService.cs b/LaDOSE.Src/LaDOSE.Service/Interface/IGameService.cs index c27c752..9e44e44 100644 --- a/LaDOSE.Src/LaDOSE.Service/Interface/IGameService.cs +++ b/LaDOSE.Src/LaDOSE.Service/Interface/IGameService.cs @@ -3,13 +3,8 @@ using LaDOSE.Entity; namespace LaDOSE.Business.Interface { - public interface IGameService + public interface IGameService : IBaseService { - 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/BaseService.cs b/LaDOSE.Src/LaDOSE.Service/Service/BaseService.cs new file mode 100644 index 0000000..df7fb4c --- /dev/null +++ b/LaDOSE.Src/LaDOSE.Service/Service/BaseService.cs @@ -0,0 +1,44 @@ +using System.Collections.Generic; +using System.Linq; +using LaDOSE.Business.Interface; +using LaDOSE.Entity.Context; +using Microsoft.EntityFrameworkCore; + +namespace LaDOSE.Business.Service +{ + + + public class BaseService : IBaseService where T : class + { + protected LaDOSEDbContext _context; + public BaseService(LaDOSEDbContext context) + { + this._context = context; + } + + public virtual IEnumerable GetAll() + { + return _context.Set().ToList(); + } + public virtual T GetById(int id) + { + return _context.Find(id); + } + public virtual T Create(T entity) + { + var added = _context.Add(entity); + return added.Entity; + } + public virtual bool Update(T entity) + { + var entityEntry = _context.Update(entity); + return _context.Entry(entityEntry).State == EntityState.Unchanged; + } + public virtual bool Delete(int id) + { + var find = _context.Find(id); + _context.Remove(find); + return _context.Entry(find).State == EntityState.Deleted; + } + } +} \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Service/Service/EventService.cs b/LaDOSE.Src/LaDOSE.Service/Service/EventService.cs new file mode 100644 index 0000000..fe092b8 --- /dev/null +++ b/LaDOSE.Src/LaDOSE.Service/Service/EventService.cs @@ -0,0 +1,26 @@ +using System; +using LaDOSE.Business.Interface; +using LaDOSE.Entity; +using LaDOSE.Entity.Context; + +namespace LaDOSE.Business.Service +{ + public class EventService : BaseService, IEventService + { + public EventService(LaDOSEDbContext context) : base(context) + { + } + + public override Event Create(Event e) + { + if (e.Id != 0) + { + throw new Exception("Id is invalid"); + } + + var eventAdded = _context.Event.Add(e); + _context.SaveChanges(); + return eventAdded.Entity; + } + } +} \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Service/Service/GameService.cs b/LaDOSE.Src/LaDOSE.Service/Service/GameService.cs index b08b16e..1e60406 100644 --- a/LaDOSE.Src/LaDOSE.Service/Service/GameService.cs +++ b/LaDOSE.Src/LaDOSE.Service/Service/GameService.cs @@ -8,64 +8,18 @@ using Microsoft.EntityFrameworkCore; namespace LaDOSE.Business.Service { - public class GameService : IGameService + public class GameService : BaseService ,IGameService { - private LaDOSEDbContext _context; - public GameService(LaDOSEDbContext context) + public GameService(LaDOSEDbContext context) : base(context) { - _context = context; } - public IEnumerable GetAll() + public override 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 diff --git a/Sql/configure.sql b/Sql/configure.sql index e65d352..76e3ad3 100644 --- a/Sql/configure.sql +++ b/Sql/configure.sql @@ -1,80 +1,169 @@ --- MySQL dump 10.13 Distrib 5.7.12, for Win64 (x86_64) +-- phpMyAdmin SQL Dump +-- version 4.6.4 +-- https://www.phpmyadmin.net/ -- --- Host: 127.0.0.1 Database: ladose --- ------------------------------------------------------ --- Server version 5.5.5-10.1.16-MariaDB +-- Host: localhost +-- Generation Time: Oct 06, 2018 at 10:12 PM +-- Server version: 5.6.40-log +-- PHP Version: 7.1.18 + +SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; +SET time_zone = "+00:00"; + /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; -/*!40101 SET NAMES utf8 */; -/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; -/*!40103 SET TIME_ZONE='+00:00' */; -/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; -/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; -/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; -/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; +/*!40101 SET NAMES utf8mb4 */; -- --- Table structure for table `applicationuser` +-- Database: `ladoseapi` -- -DROP TABLE IF EXISTS `applicationuser`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `applicationuser` ( - `id` int(11) NOT NULL AUTO_INCREMENT, +-- -------------------------------------------------------- + +-- +-- Table structure for table `ApplicationUser` +-- + +CREATE TABLE `ApplicationUser` ( + `Id` int(11) NOT NULL, `FirstName` varchar(45) DEFAULT NULL, `LastName` varchar(45) DEFAULT NULL, - `Username` varchar(45) DEFAULT NULL, + `UserName` varchar(45) DEFAULT NULL, `PasswordHash` blob, - `PasswordSalt` blob, - PRIMARY KEY (`id`) -) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `game` --- - -DROP TABLE IF EXISTS `game`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `game` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `name` varchar(45) CHARACTER SET utf8 DEFAULT NULL, - `imgurl` varchar(255) CHARACTER SET utf8 DEFAULT NULL, - PRIMARY KEY (`id`), - UNIQUE KEY `name_UNIQUE` (`name`) -) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `saison` --- - -DROP TABLE IF EXISTS `saison`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `saison` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `name` varchar(45) NOT NULL, - `datedebut` datetime NOT NULL, - `datefin` datetime DEFAULT NULL, - PRIMARY KEY (`id`), - UNIQUE KEY `name_UNIQUE` (`name`) + `PasswordSalt` blob ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -/*!40101 SET character_set_client = @saved_cs_client */; + +-- -------------------------------------------------------- -- --- Table structure for table `user` +-- Table structure for table `Event` -- +CREATE TABLE `Event` ( + `Id` int(11) NOT NULL, + `Name` varchar(255) NOT NULL, + `Date` datetime NOT NULL, + `SeasonId` int(11) NOT NULL, + `Ranking` tinyint(4) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +-- -------------------------------------------------------- + +-- +-- Table structure for table `Game` +-- + +CREATE TABLE `Game` ( + `Id` int(11) NOT NULL, + `Name` varchar(45) CHARACTER SET utf8 DEFAULT NULL, + `ImgUrl` varchar(255) CHARACTER SET utf8 DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; + +-- -------------------------------------------------------- + +-- +-- Table structure for table `Season` +-- + +CREATE TABLE `Season` ( + `Id` int(11) NOT NULL, + `Name` varchar(45) DEFAULT NULL, + `StartDate` datetime DEFAULT NULL, + `EndDate` datetime DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +-- -------------------------------------------------------- + +-- +-- Table structure for table `SeasonGame` +-- + +CREATE TABLE `SeasonGame` ( + `SeasonId` int(11) NOT NULL, + `GameId` int(11) NOT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +-- +-- Indexes for dumped tables +-- + +-- +-- Indexes for table `ApplicationUser` +-- +ALTER TABLE `ApplicationUser` + ADD PRIMARY KEY (`Id`); + +-- +-- Indexes for table `Event` +-- +ALTER TABLE `Event` + ADD PRIMARY KEY (`Id`), + ADD KEY `SeasonPK_idx` (`SeasonId`); + +-- +-- Indexes for table `Game` +-- +ALTER TABLE `Game` + ADD PRIMARY KEY (`Id`), + ADD UNIQUE KEY `name_UNIQUE` (`Name`); + +-- +-- Indexes for table `Season` +-- +ALTER TABLE `Season` + ADD PRIMARY KEY (`Id`), + ADD UNIQUE KEY `Name_UNIQUE` (`Name`); + +-- +-- Indexes for table `SeasonGame` +-- +ALTER TABLE `SeasonGame` + ADD PRIMARY KEY (`SeasonId`,`GameId`), + ADD KEY `GamePK_idx` (`GameId`); + +-- +-- AUTO_INCREMENT for dumped tables +-- + +-- +-- AUTO_INCREMENT for table `ApplicationUser` +-- +ALTER TABLE `ApplicationUser` + MODIFY `Id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4; +-- +-- AUTO_INCREMENT for table `Event` +-- +ALTER TABLE `Event` + MODIFY `Id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2; +-- +-- AUTO_INCREMENT for table `Game` +-- +ALTER TABLE `Game` + MODIFY `Id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3; +-- +-- AUTO_INCREMENT for table `Season` +-- +ALTER TABLE `Season` + MODIFY `Id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2; +-- +-- Constraints for dumped tables +-- + +-- +-- Constraints for table `Event` +-- +ALTER TABLE `Event` + ADD CONSTRAINT `SeasonsPK` FOREIGN KEY (`SeasonId`) REFERENCES `Season` (`Id`) ON DELETE NO ACTION ON UPDATE NO ACTION; + +-- +-- Constraints for table `SeasonGame` +-- +ALTER TABLE `SeasonGame` + ADD CONSTRAINT `GamePK` FOREIGN KEY (`GameId`) REFERENCES `Game` (`Id`) ON DELETE NO ACTION ON UPDATE NO ACTION, + ADD CONSTRAINT `SeasonPK` FOREIGN KEY (`SeasonId`) REFERENCES `Season` (`Id`) ON DELETE NO ACTION ON UPDATE NO ACTION; -/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; -/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; -/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; -/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;