Implémentation de l'ajout, la modification et la suppression d'une note

develop
Yanaël GRETTE 4 years ago
parent 4e78c28d10
commit a07a78af87
  1. 43
      Controllers/NotesApi.cs
  2. 7
      IServices/INoteService.cs
  3. 44
      Services/NoteService.cs

@ -20,6 +20,7 @@ using Microsoft.AspNetCore.Authorization;
using IO.Swagger.DTO;
using EPAServeur.Services;
using EPAServeur.IServices;
using EPAServeur.Models.Notes;
namespace IO.Swagger.Controllers
{
@ -53,16 +54,18 @@ namespace IO.Swagger.Controllers
[SwaggerResponse(statusCode: 404, type: typeof(ErreurDTO), description: "Ressource n'a pas été trouvée")]
public virtual IActionResult DeleteNote([FromRoute][Required]int? idNote)
{
//TODO: Uncomment the next line to return response 204 or use other options such as return this.NotFound(), return this.BadRequest(..), ...
// return StatusCode(204);
//TODO: Uncomment the next line to return response 403 or use other options such as return this.NotFound(), return this.BadRequest(..), ...
// return StatusCode(403, default(ErreurDTO));
//TODO: Uncomment the next line to return response 404 or use other options such as return this.NotFound(), return this.BadRequest(..), ...
// return StatusCode(404, default(ErreurDTO));
throw new NotImplementedException();
if(!noteService.SupprimerNote(idNote))
{
ErreurDTO erreur = new ErreurDTO()
{
Code = "404",
Message = "Aucune note trouvé"
};
return NotFound(erreur);
};
return NoContent();
}
/// <summary>
@ -223,14 +226,11 @@ namespace IO.Swagger.Controllers
[SwaggerOperation("NouvelleNote")]
[SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")]
public virtual IActionResult NouvelleNote([FromBody]DetailsNoteDTO body)
{
//TODO: Uncomment the next line to return response 201 or use other options such as return this.NotFound(), return this.BadRequest(..), ...
// return StatusCode(201);
{
//TODO: Uncomment the next line to return response 403 or use other options such as return this.NotFound(), return this.BadRequest(..), ...
// return StatusCode(403, default(ErreurDTO));
throw new NotImplementedException();
Note nouvelleNote = noteService.AjouterNote(body);
return Created("",nouvelleNote);
}
/// <summary>
@ -249,17 +249,16 @@ namespace IO.Swagger.Controllers
[SwaggerOperation("UpdateNote")]
[SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")]
public virtual IActionResult UpdateNote([FromBody]DetailsNoteDTO body, [FromRoute][Required]int? idNote)
{
//TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ...
// return StatusCode(200);
//TODO: Uncomment the next line to return response 201 or use other options such as return this.NotFound(), return this.BadRequest(..), ...
// return StatusCode(201);
{
//TODO: Uncomment the next line to return response 403 or use other options such as return this.NotFound(), return this.BadRequest(..), ...
// return StatusCode(403, default(ErreurDTO));
throw new NotImplementedException();
Note note = noteService.UpdateNote(idNote, body);
if (note == null)
note = noteService.AjouterNote(body);
return Ok(note);
//TODO: Uncomment the next line to return response 201 or use other options such as return this.NotFound(), return this.BadRequest(..), ...
// return StatusCode(201);
}
}
}

@ -1,4 +1,5 @@
using IO.Swagger.DTO;
using EPAServeur.Models.Notes;
using IO.Swagger.DTO;
using Org.BouncyCastle.Bcpg.OpenPgp;
using System;
using System.Collections.Generic;
@ -14,8 +15,8 @@ namespace EPAServeur.IServices
public IEnumerable<AffichageNoteDTO> GetNotesByAuteur(Guid? idAuteur, bool? asc, int? numPage, int? parPAge, string texte, string tri);
public DetailsNoteDTO GetNoteById(int? idNote);
public IEnumerable<AffichageNoteDTO> GetNotesByCollaborateur(Guid? idAuteur, Guid? idCollaborateur, bool? asc, int? numPage, int? parPAge, string texte, string tri);
public bool AjouterNote(DetailsNoteDTO nouvelleNote);
public Note AjouterNote(DetailsNoteDTO nouvelleNote);
public bool SupprimerNote(int? idNote);
public void UpdateNote(int? idNote, DetailsNoteDTO note);
public Note UpdateNote(int? idNote, DetailsNoteDTO note);
}
}

@ -22,9 +22,14 @@ namespace EPAServeur.Services
context = _context;
}
public bool AjouterNote(DetailsNoteDTO nouvelleNote)
public Note AjouterNote(DetailsNoteDTO nouvelleNote)
{
throw new NotImplementedException();
if (!IsDetailsNoteValide(nouvelleNote))
return null;
Note note = DetailsNoteDTOToNouvelleNote(nouvelleNote);
context.Note.Add(note);
context.SaveChanges();
return note;
}
public IEnumerable<AffichageNoteDTO> GetNotesByCollaborateur(Guid? idAuteur, Guid? idCollaborateur, bool? asc, int? numPage, int? parPage, string texte, string tri)
@ -48,8 +53,10 @@ namespace EPAServeur.Services
public DetailsNoteDTO GetNoteById(int? idNote)
{
DetailsNoteDTO details = NoteToDetailSDTO(context.Note.Where(c => c.Id == idNote).FirstOrDefault());
return details;
Note note = context.Note.Find(idNote);
if (note == null)
return null;
return NoteToDetailSDTO(note);
/*return (from n in context.Note
where n.Id == idNote
select NoteToDetailSDTO(n)).ToList().FirstOrDefault();*/
@ -88,12 +95,32 @@ namespace EPAServeur.Services
public bool SupprimerNote(int? idNote)
{
throw new NotImplementedException();
Note note = context.Note.Find(idNote);
if (note == null)
return false;
context.Remove(note);
context.SaveChanges();
return true;
}
public void UpdateNote(int? idNote, DetailsNoteDTO note)
public Note UpdateNote(int? idNote, DetailsNoteDTO note)
{
throw new NotImplementedException();
if (!IsDetailsNoteValide(note))
return null;
Note noteToUpdate = context.Note.Find(idNote);
if (noteToUpdate == null)
return AjouterNote(note);
noteToUpdate.Titre = note.Titre;
noteToUpdate.Texte = note.Texte;
noteToUpdate.DateUpdate = DateTime.Now;
context.SaveChanges();
return noteToUpdate;
}
private bool IsDetailsNoteValide(DetailsNoteDTO note)
{
return !(note == null || note.IdAuteur == null || note.Collaborateur == null || note.Collaborateur.Id == null || note.Titre == null || note.Texte == null) ;
}
//Object to DTO
@ -135,7 +162,8 @@ namespace EPAServeur.Services
IdCollaborateur = detailsNoteDTO.Collaborateur.Id.Value,
Texte = detailsNoteDTO.Texte,
Titre = detailsNoteDTO.Titre,
DateCreation = DateTime.Now
DateCreation = DateTime.Now,
DateUpdate = DateTime.Now
};
return note;
}

Loading…
Cancel
Save