Generic Controller Test

This commit is contained in:
2018-10-12 20:52:59 +02:00
parent 39075051b2
commit e47c1ccb5e
12 changed files with 102 additions and 58 deletions

View File

@@ -11,32 +11,17 @@ namespace LaDOSE.Api.Controllers
{
[Produces("application/json")]
[Route("api/[controller]")]
public class EventController : Controller
public class EventController : GenericController<IEventService, Event>
{
private IEventService _eventService;
public EventController(IEventService eventService)
public EventController(IEventService service) : base(service)
{
_eventService = eventService;
}
[HttpPost]
public Event Post([FromBody]Event dto)
{
return _eventService.Create(dto);
}
[HttpGet("{id}")]
public Event Get(int id)
{
return _eventService.GetById(id);
}
[HttpGet("Generate/{eventId}/{wpEventId}")]
public bool GenerateChallonge(int eventId, int wpEventId)
{
return _eventService.CreateChallonge(eventId, wpEventId);
return _service.CreateChallonge(eventId, wpEventId);
}
}

View File

@@ -13,36 +13,10 @@ namespace LaDOSE.Api.Controllers
[Authorize]
[Route("api/[controller]")]
[Produces("application/json")]
public class GameController : ControllerBase
public class GameController : GenericController<IGameService, Game>
{
private readonly IGameService _gameService;
public GameController(IGameService gameService)
public GameController(IGameService service) : base(service)
{
_gameService = gameService;
}
// GET api/Game
[HttpGet]
public List<Game> Get()
{
return _gameService.GetAll().ToList();
}
// GET api/Game/5
[HttpGet("{id}")]
public Game Get(int id)
{
return _gameService.GetById(id);
}
[HttpPut()]
public bool Put(Game game)
{
return _gameService.Update(game);
}
}
}

View File

@@ -0,0 +1,37 @@
using System.Collections.Generic;
using System.Linq;
using LaDOSE.Business.Interface;
using LaDOSE.Entity;
using Microsoft.AspNetCore.Mvc;
namespace LaDOSE.Api.Controllers
{
public class GenericController<T,TU> :Controller where TU : Entity.Context.Entity where T : IBaseService<TU>
{
protected T _service;
public GenericController(T service)
{
_service = service;
}
[HttpPost]
public TU Post([FromBody]TU dto)
{
return _service.Create(dto);
}
[HttpGet]
public List<TU> Get()
{
return _service.GetAll().ToList();
}
[HttpGet("{id}")]
public TU Get(int id)
{
return _service.GetById(id);
}
}
}

View File

@@ -0,0 +1,18 @@
using LaDOSE.Business.Interface;
using LaDOSE.Business.Service;
using LaDOSE.Entity;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace LaDOSE.Api.Controllers
{
[Authorize]
[Route("api/[controller]")]
[Produces("application/json")]
public class SeasonController : GenericController<ISeasonService,Season>
{
public SeasonController(ISeasonService service) : base(service)
{
}
}
}

View File

@@ -97,6 +97,7 @@ namespace LaDOSE.Api
services.AddScoped<IUserService, UserService>();
services.AddScoped<IGameService, GameService>();
services.AddScoped<IEventService, EventService>();
services.AddScoped<ISeasonService, SeasonService>();
services.AddTransient<IChallongeProvider>(p => new ChallongeProvider(this.Configuration["ApiKey:ChallongeApiKey"]));
}

View File

@@ -0,0 +1,10 @@
using System.ComponentModel.DataAnnotations;
namespace LaDOSE.Entity.Context
{
public class Entity
{
[Key]
public int Id { get; set; }
}
}

View File

@@ -4,10 +4,10 @@ using System.ComponentModel.DataAnnotations;
namespace LaDOSE.Entity
{
public class Event
public class Event : Context.Entity
{
[Key]
public int Id { get; set; }
//[Key]
//public int Id { get; set; }
public string Name { get; set; }
public DateTime Date { get; set; }
public int SeasonId { get; set; }

View File

@@ -1,6 +1,6 @@
namespace LaDOSE.Entity
{
public class EventGame
public class EventGame
{
public int EventId { get; set; }

View File

@@ -4,10 +4,8 @@ using System.ComponentModel.DataAnnotations;
namespace LaDOSE.Entity
{
public class Game
public class Game : Context.Entity
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string ImgUrl { get; set; }

View File

@@ -4,10 +4,9 @@ using System.ComponentModel.DataAnnotations;
namespace LaDOSE.Entity
{
public class Season
public class Season : Context.Entity
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public DateTime StartDate { get; set; }

View File

@@ -0,0 +1,9 @@
using LaDOSE.Entity;
namespace LaDOSE.Business.Interface
{
public interface ISeasonService : IBaseService<Season>
{
}
}

View File

@@ -0,0 +1,13 @@
using LaDOSE.Business.Interface;
using LaDOSE.Entity;
using LaDOSE.Entity.Context;
namespace LaDOSE.Business.Service
{
public class SeasonService : BaseService<Season>, ISeasonService
{
public SeasonService(LaDOSEDbContext context) : base(context)
{
}
}
}