Generic Service
JSON Reference loop Events
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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<LaDOSEDbContext>( // 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 =>
|
||||
|
||||
@@ -7,6 +7,7 @@ namespace LaDOSE.Entity.Context
|
||||
public DbSet<Game> Game { get; set; }
|
||||
public DbSet<ApplicationUser> ApplicationUser { get; set; }
|
||||
public DbSet<Season> Season { get; set; }
|
||||
public DbSet<Event> Event { get; set; }
|
||||
//public DbSet<SeasonGame> SeasonGame { get; set; }
|
||||
|
||||
public LaDOSEDbContext(DbContextOptions options) : base(options)
|
||||
@@ -21,7 +22,10 @@ namespace LaDOSE.Entity.Context
|
||||
modelBuilder.Entity<SeasonGame>()
|
||||
.HasKey(t => new { t.SeasonId, t.GameId });
|
||||
|
||||
|
||||
modelBuilder.Entity<Event>()
|
||||
.HasOne(s => s.Season)
|
||||
.WithMany(p => p.Event)
|
||||
.HasForeignKey(fk => fk.SeasonId);
|
||||
|
||||
modelBuilder.Entity<SeasonGame>()
|
||||
.HasOne(pt => pt.Season)
|
||||
|
||||
17
LaDOSE.Src/LaDOSE.Entity/Event.cs
Normal file
17
LaDOSE.Src/LaDOSE.Entity/Event.cs
Normal file
@@ -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; }
|
||||
|
||||
}
|
||||
}
|
||||
@@ -14,5 +14,7 @@ namespace LaDOSE.Entity
|
||||
public DateTime EndDate { get; set; }
|
||||
|
||||
public virtual IEnumerable<SeasonGame> Games { get; set; }
|
||||
|
||||
public virtual IEnumerable<Event> Event { get; set; }
|
||||
}
|
||||
}
|
||||
13
LaDOSE.Src/LaDOSE.Service/Interface/IBaseService.cs
Normal file
13
LaDOSE.Src/LaDOSE.Service/Interface/IBaseService.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace LaDOSE.Business.Interface
|
||||
{
|
||||
public interface IBaseService<T> where T : class
|
||||
{
|
||||
IEnumerable<T> GetAll();
|
||||
T GetById(int id);
|
||||
T Create(T entity);
|
||||
bool Update(T entity);
|
||||
bool Delete(int id);
|
||||
}
|
||||
}
|
||||
9
LaDOSE.Src/LaDOSE.Service/Interface/IEventService.cs
Normal file
9
LaDOSE.Src/LaDOSE.Service/Interface/IEventService.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using LaDOSE.Entity;
|
||||
|
||||
namespace LaDOSE.Business.Interface
|
||||
{
|
||||
public interface IEventService : IBaseService<Event>
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -3,13 +3,8 @@ using LaDOSE.Entity;
|
||||
|
||||
namespace LaDOSE.Business.Interface
|
||||
{
|
||||
public interface IGameService
|
||||
public interface IGameService : IBaseService<Game>
|
||||
{
|
||||
|
||||
IEnumerable<Game> GetAll();
|
||||
Game GetById(int id);
|
||||
Game Create(Game game);
|
||||
bool Update(Game game);
|
||||
void Delete(int id);
|
||||
}
|
||||
}
|
||||
44
LaDOSE.Src/LaDOSE.Service/Service/BaseService.cs
Normal file
44
LaDOSE.Src/LaDOSE.Service/Service/BaseService.cs
Normal file
@@ -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<T> : IBaseService<T> where T : class
|
||||
{
|
||||
protected LaDOSEDbContext _context;
|
||||
public BaseService(LaDOSEDbContext context)
|
||||
{
|
||||
this._context = context;
|
||||
}
|
||||
|
||||
public virtual IEnumerable<T> GetAll()
|
||||
{
|
||||
return _context.Set<T>().ToList();
|
||||
}
|
||||
public virtual T GetById(int id)
|
||||
{
|
||||
return _context.Find<T>(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<T>(id);
|
||||
_context.Remove(find);
|
||||
return _context.Entry(find).State == EntityState.Deleted;
|
||||
}
|
||||
}
|
||||
}
|
||||
26
LaDOSE.Src/LaDOSE.Service/Service/EventService.cs
Normal file
26
LaDOSE.Src/LaDOSE.Service/Service/EventService.cs
Normal file
@@ -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<Event>, 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,64 +8,18 @@ using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace LaDOSE.Business.Service
|
||||
{
|
||||
public class GameService : IGameService
|
||||
public class GameService : BaseService<Game> ,IGameService
|
||||
{
|
||||
private LaDOSEDbContext _context;
|
||||
|
||||
public GameService(LaDOSEDbContext context)
|
||||
public GameService(LaDOSEDbContext context) : base(context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public IEnumerable<Game> GetAll()
|
||||
public override IEnumerable<Game> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user