diff --git a/Sources/BowlingApi/BowlingApi.csproj b/Sources/BowlingApi/BowlingApi.csproj index 7f0352f..491442d 100644 --- a/Sources/BowlingApi/BowlingApi.csproj +++ b/Sources/BowlingApi/BowlingApi.csproj @@ -9,8 +9,9 @@ + - + @@ -25,6 +26,9 @@ + + + @@ -33,5 +37,4 @@ - diff --git a/Sources/BowlingApi/Controllers/PartieController.cs b/Sources/BowlingApi/Controllers/PartieController.cs index 848d695..cd67275 100644 --- a/Sources/BowlingApi/Controllers/PartieController.cs +++ b/Sources/BowlingApi/Controllers/PartieController.cs @@ -1,19 +1,22 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; +using System; +using DTOs; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using BowlingEF.Entities; +using BowlingLib.Model; using BowlingService; using BowlingService.Interfaces; -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; - -namespace BowlingApi.Controllers +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; + +namespace BowlingApi.Controllers { - - - [Route("api/[controller]")] - [ApiController] - public class PartieController : ControllerBase + + + [Route("api/[controller]")] + [ApiController] + public class PartieController : ControllerBase { @@ -29,35 +32,103 @@ namespace BowlingApi.Controllers // GET: api/Partie [HttpGet] - public IActionResult Get() + public async Task Get() + { + try + { + var result = await _partieService.GetAll(); + if (result == null) + { + return NotFound(); + } + return Ok(result); + } + catch (Exception e) + { + return StatusCode(StatusCodes.Status500InternalServerError, e.Message); + throw; + } + //var result = _partieService.GetAll().Result; + //return Ok(result); + } + + // GET: api/Partie/djon + [HttpGet("{id}")] + public async Task Get(int id) + { + // return Ok(_partieService.GetDataWithName(name)); + + + try + { + if (id == null) + return BadRequest("Le nom de la partie est obligatoire"); + + var result = _partieService.GetDataWithId(id).Result; + if (result == null) + { + return NotFound(); + } + return Ok(result); + } + catch (Exception e) + { + return StatusCode(StatusCodes.Status500InternalServerError, e.Message); + throw; + } + + } + + // POST: api/Partie + [HttpPost] + public async Task> Post([FromBody] PartieDTO parti) { - var result = _partieService.GetAll().Result; - return Ok(result); + + + try + { + if (parti == null) + { + return BadRequest("partie est obligatoire"); + } + var createdpartie = _partieService.Add(parti).Result; + return CreatedAtAction(nameof(Get), new { id = parti.Id }, createdpartie); + + } + catch (Exception e) + { + return StatusCode(StatusCodes.Status500InternalServerError, e.Message); + throw; + } + } - // GET: api/Partie/5 - [HttpGet("{name}")] - public IActionResult Get(string name) + // PUT: api/Partie/5 + [HttpPut("{id}")] + public async Task> Put(string name, [FromBody] PartieDTO partie) { - return Ok(_partieService.GetDataWithName(name)); - } - - // POST: api/Partie - [HttpPost] - public void Post([FromBody] string value) - { - } - - // PUT: api/Partie/5 - [HttpPut("{id}")] - public void Put(int id, [FromBody] string value) - { - } - - // DELETE: api/Partie/5 - [HttpDelete("{id}")] - public void Delete(int id) - { - } - } -} + try + { + if (partie == null) + return BadRequest("La partie est obligatoire"); + + var updatepartie = _partieService.Update(partie); + if (updatepartie.Result == null) + { + return NotFound(); + } + + return Ok(updatepartie); + } + catch (Exception e) + { + StatusCode(StatusCodes.Status500InternalServerError, e.Message); + throw; + } + + } + + + } +} + diff --git a/Sources/BowlingApi/Properties/launchSettings.json b/Sources/BowlingApi/Properties/launchSettings.json index fc48bb7..cfd4403 100644 --- a/Sources/BowlingApi/Properties/launchSettings.json +++ b/Sources/BowlingApi/Properties/launchSettings.json @@ -29,8 +29,6 @@ }, "RestFull": { "commandName": "Project", - "dotnetRunMessages": true, - "launchBrowser": false, "applicationUrl": "https://localhost:5001", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" diff --git a/Sources/BowlingApi/bowling.db b/Sources/BowlingApi/bowling.db index b257dc9..e0616d7 100644 Binary files a/Sources/BowlingApi/bowling.db and b/Sources/BowlingApi/bowling.db differ diff --git a/Sources/BowlingRepository/Interface/IpartieRepository.cs b/Sources/BowlingRepository/Interface/IpartieRepository.cs index 9381a82..a1d2df6 100644 --- a/Sources/BowlingRepository/Interface/IpartieRepository.cs +++ b/Sources/BowlingRepository/Interface/IpartieRepository.cs @@ -5,12 +5,12 @@ namespace BowlingRepository.Interface { public interface IpartieRepository { - public Task Add(PartieEntity _partie); - public Task Delete(PartieEntity _partie); + public Task Add(PartieEntity _partie); + public Task Delete(long id); public Task Update(PartieEntity _partie); public Task> GetAll(); public Task> GetAllWithDate(DateTime date); - public Task GetDataWithName(string nom); + public Task GetDataWithId(int id); } } diff --git a/Sources/BowlingRepository/Interface/PartieRepository.cs b/Sources/BowlingRepository/Interface/PartieRepository.cs index 43f8020..9cd4a1d 100644 --- a/Sources/BowlingRepository/Interface/PartieRepository.cs +++ b/Sources/BowlingRepository/Interface/PartieRepository.cs @@ -5,7 +5,7 @@ using Microsoft.EntityFrameworkCore; namespace BowlingRepository.Interface { - public class PartieRepository:IpartieRepository + public class PartieRepository : IpartieRepository { private readonly BowlingContext _context; public PartieRepository() @@ -13,25 +13,30 @@ namespace BowlingRepository.Interface _context = new BowlingContext(); } - public async Task Add(PartieEntity partie) + public async Task Add(PartieEntity partie) { - _context.Parties.Add( partie); - return await _context.SaveChangesAsync() > 0; + + var result = await _context.Parties.AddAsync(partie); + await _context.SaveChangesAsync(); + return result.Entity; + + //_context.Parties.Add( partie); + //return await _context.SaveChangesAsync() > 0; } - public async Task Delete(PartieEntity _partie) + public async Task Delete(long id) { using (var context = new BowlingContext()) { - PartieEntity entity = context.Parties.Find(_partie.Id); + PartieEntity entity = context.Parties.Find(id); context.Parties.Remove(entity); return await context.SaveChangesAsync() > 0; } } - + public async Task> GetAll() { @@ -43,12 +48,13 @@ namespace BowlingRepository.Interface throw new NotImplementedException(); } - public Task GetDataWithName(string nom) + public async Task GetDataWithId(int id) { - // return await _context.Parties.FirstOrDefaultAsync(n => n == nom); - throw new NotImplementedException(); + var data = await _context.Parties.FirstOrDefaultAsync(n => n.Id == id); + return data; } + public async Task Update(PartieEntity _partie) { return await _context.SaveChangesAsync() > 0; @@ -56,3 +62,4 @@ namespace BowlingRepository.Interface } } + diff --git a/Sources/BowlingRepository/JoueurRepository.cs b/Sources/BowlingRepository/JoueurRepository.cs index 3e0a513..7bedefe 100644 --- a/Sources/BowlingRepository/JoueurRepository.cs +++ b/Sources/BowlingRepository/JoueurRepository.cs @@ -30,7 +30,7 @@ public class JoueurRepository:IJoueurRepository public async Task Update(JoueurEntity joueur) { - return await _context.SaveChangesAsync() > 0; + return await _context.SaveChangesAsync() > 0; } public async Task GetJoueur(long id) diff --git a/Sources/BowlingService/Interfaces/IPartieService.cs b/Sources/BowlingService/Interfaces/IPartieService.cs index dd033c7..29e47ab 100644 --- a/Sources/BowlingService/Interfaces/IPartieService.cs +++ b/Sources/BowlingService/Interfaces/IPartieService.cs @@ -4,14 +4,14 @@ using DTOs; namespace BowlingService.Interfaces { - public interface IpartieService + public interface IpartieService { - public Task Add(PartieEntity _partie); - public Task Delete(PartieEntity _partie); - public Task Update(PartieEntity _partie); + public Task Add(PartieDTO _partie); + public Task Delete(PartieDTO _partie); + public Task Update(PartieDTO _partie); public Task> GetAll(); public Task> GetAllWithDate(DateTime date); - public Task GetDataWithName(string nom); + public Task GetDataWithId(int id); } } diff --git a/Sources/BowlingService/Interfaces/PartieService.cs b/Sources/BowlingService/Interfaces/PartieService.cs index ebf286b..9ba6d33 100644 --- a/Sources/BowlingService/Interfaces/PartieService.cs +++ b/Sources/BowlingService/Interfaces/PartieService.cs @@ -1,4 +1,5 @@ using System; +using System.Xml.Linq; using AutoMapper; using BowlingEF.Context; using BowlingEF.Entities; @@ -7,51 +8,74 @@ using BowlingRepository; using BowlingRepository.Interface; using DTOs; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; namespace BowlingService.Interfaces { - public class PartieService:IpartieService - { + public class PartieService : IpartieService + { private readonly IpartieRepository _IpartieRepository; + private readonly ILogger _logger; + private readonly IMapper _mapper; - public PartieService(IpartieRepository ipartieRepository, IMapper mapper) + public PartieService(IpartieRepository ipartieRepository, IMapper mapper, ILogger logger) { _IpartieRepository = ipartieRepository; _mapper = mapper; + _logger = logger; } public PartieService() - { + { - } - private readonly IMapper _mapper; + } + + //Add - public async Task Add(PartieDTO _partie) + +public async Task Add(PartieDTO _partie) { - bool result = false; + + PartieDTO result = null; using (var context = new BowlingContext()) { PartieEntity entity = _mapper.Map(_partie); - context.Parties.Add(entity); + //context.Parties.Add(entity); try { - var data = await context.SaveChangesAsync(); - result = data == 1; + //var data = await context.SaveChangesAsync(); + result =_mapper.Map(await _IpartieRepository.Add(entity)); + _logger.LogInformation("A new player was added : {player}", _partie.Id); + } catch (Exception ex) { - Console.WriteLine(ex); + _logger.LogError(ex, "Error while adding new player : {player}", _partie.Id); throw; } } return result; } + //Delete - - public async Task Delete(PartieEntity _partie) + public async Task Delete(PartieDTO _partie) { - return await _IpartieRepository.Delete(_partie); + // return await _IpartieRepository.Delete(_partie.Id); + + var result = false; + try + { + result = await _IpartieRepository.Delete(_partie.Id); + _logger.LogInformation("la partie est supprimer", _partie.Id); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error while deleting player : {player}", _partie.Id); + throw; + } + return result; + } public async Task> GetAll() @@ -80,20 +104,45 @@ namespace BowlingService.Interfaces throw new NotImplementedException(); } - public Task GetDataWithName(string nom) + public async Task GetDataWithId(int id) { - throw new NotImplementedException(); - } + PartieDTO _partie = null; - public Task Update(PartieEntity _partie) - { - throw new NotImplementedException(); + try + { + var partientity = await _IpartieRepository.GetDataWithId(id); + _partie = _mapper.Map(partientity); + _logger.LogInformation("partie was retrieved : {partie}", id); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error while retrieving partie : {partie}", id); + throw; + } + return _partie; } - public Task Add(PartieEntity _partie) + + //Update + + public async Task Update(PartieDTO _partie) { - throw new NotImplementedException(); + + bool result = false; + using (var context = new BowlingContext()) + { + PartieEntity entity = _mapper.Map(_partie); + entity.Date = _partie.Date; + entity.Score = _partie.Score; + result = _IpartieRepository.Update(entity).Result; + + } + return result; + } + + } } +