From 2b7afa9c094a2a5b36edf4b367d4a642c15c4e8e Mon Sep 17 00:00:00 2001 From: Ruidy Nemausat Date: Thu, 27 Feb 2020 10:10:05 +0100 Subject: [PATCH] wrote finer DTOs to avoid circular dependencies --- .gitignore | 1 + Controllers/TicketsController.cs | 2 +- DTOs/{ => AppUser}/AppUserDTO.cs | 8 ++--- DTOs/AppUser/AppUserDTORead.cs | 44 +++++++++++++++++++++++++++ DTOs/{ => Project}/ProjectDTO.cs | 12 ++++---- DTOs/Project/ProjectDTORead.cs | 38 +++++++++++++++++++++++ DTOs/{ => Tickets}/TicketDTO.cs | 8 ++--- DTOs/Tickets/TicketDTORead.cs | 52 ++++++++++++++++++++++++++++++++ Models/Activity.cs | 2 +- Models/Interfaces/ITask.cs | 1 - Models/Note.cs | 2 +- Models/Project.cs | 22 -------------- Models/Ticket.cs | 10 ++---- README.md | 2 +- Scripts/apiQueries.sh | 12 ++++++-- Scripts/response.http | 0 16 files changed, 166 insertions(+), 50 deletions(-) rename DTOs/{ => AppUser}/AppUserDTO.cs (86%) create mode 100644 DTOs/AppUser/AppUserDTORead.cs rename DTOs/{ => Project}/ProjectDTO.cs (77%) create mode 100644 DTOs/Project/ProjectDTORead.cs rename DTOs/{ => Tickets}/TicketDTO.cs (84%) create mode 100644 DTOs/Tickets/TicketDTORead.cs delete mode 100644 Scripts/response.http diff --git a/.gitignore b/.gitignore index e2a1839..f64aa1b 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ Data/UnitOfWork.cs Data/*Repository.cs Migrations/ Properties/ +Scripts/response.json Tests/TicketManager.Tests/UnitTests/ControllersTests/ControllerTests.cs Tests/TicketManager.Tests/UnitTests/ControllersTests/SeedDb.cs diff --git a/Controllers/TicketsController.cs b/Controllers/TicketsController.cs index cc94ec6..35d9ae1 100644 --- a/Controllers/TicketsController.cs +++ b/Controllers/TicketsController.cs @@ -10,7 +10,7 @@ using TicketManager.Models; namespace TicketManager.Controllers { - [Authorize] + // [Authorize] [Route("api/v1/[controller]")] [ApiController] public class TicketsController : ControllerBase diff --git a/DTOs/AppUserDTO.cs b/DTOs/AppUser/AppUserDTO.cs similarity index 86% rename from DTOs/AppUserDTO.cs rename to DTOs/AppUser/AppUserDTO.cs index cafa9f5..c31dc8d 100644 --- a/DTOs/AppUserDTO.cs +++ b/DTOs/AppUser/AppUserDTO.cs @@ -19,8 +19,8 @@ namespace TicketManager.DTO CreationDate = user.CreationDate; Picture = user.Picture; Activities = user.Activities; - Projects = user.GetProjects().Select(u => new ProjectDTO(u)).ToList(); - Tickets = user.GetTickets().Select(u => new TicketDTO(u)).ToList(); + Projects = user.GetProjects().Select(u => new ProjectDTORead(u)).ToList(); + Tickets = user.GetTickets().Select(u => new TicketDTORead(u)).ToList(); } public Guid Id { get; set; } @@ -46,8 +46,8 @@ namespace TicketManager.DTO public List Activities { get; set; } = new List(); - public List Projects { get; set; } = new List(); + public List Projects { get; set; } = new List(); - public List Tickets { get; set; } = new List(); + public List Tickets { get; set; } = new List(); } } \ No newline at end of file diff --git a/DTOs/AppUser/AppUserDTORead.cs b/DTOs/AppUser/AppUserDTORead.cs new file mode 100644 index 0000000..a814720 --- /dev/null +++ b/DTOs/AppUser/AppUserDTORead.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using TicketManager.Models; + +namespace TicketManager.DTO +{ + public class AppUserDTORead + { + public AppUserDTORead(AppUser user) + { + Id = user.Id; + FirstName = user.FirstName; + LastName = user.LastName; + Presentation = user.Presentation; + Email = user.Email; + Phone = user.Phone; + CreationDate = user.CreationDate; + Picture = user.Picture; + } + + public Guid Id { get; set; } + + public string FirstName { get; set; } + + public string LastName { get; set; } + + public string FullName => $"{FirstName} {LastName}"; + + public string Presentation { get; set; } + + [DataType(DataType.EmailAddress)] + public string Email { get; set; } + + [DataType(DataType.PhoneNumber)] + public string Phone { get; set; } + + [DataType(DataType.Date)] + public DateTime CreationDate { get; private set; } = DateTime.Now; + + public string Picture { get; set; } + } +} \ No newline at end of file diff --git a/DTOs/ProjectDTO.cs b/DTOs/Project/ProjectDTO.cs similarity index 77% rename from DTOs/ProjectDTO.cs rename to DTOs/Project/ProjectDTO.cs index 33972be..8261d87 100644 --- a/DTOs/ProjectDTO.cs +++ b/DTOs/Project/ProjectDTO.cs @@ -16,9 +16,9 @@ namespace TicketManager.DTO EndingDate = project.EndingDate; Progression = project.Progression; Status = project.Status.ToString(); - // Manager = project.Manager != null ? new AppUserDTO(project.Manager) : null; - Users = project.GetMembers().Select(u => new AppUserDTO(u)).ToList(); - Tickets = project.Tickets.Select(t => new TicketDTO(t)).ToList(); + // Manager = project.Manager != null ? new AppUserDTORead(project.Manager) : null; + Users = project.GetMembers().Select(u => new AppUserDTORead(u)).ToList(); + Tickets = project.Tickets.Select(t => new TicketDTORead(t)).ToList(); Activities = project.Activities; Files = project.Files; } @@ -37,11 +37,11 @@ namespace TicketManager.DTO public string Status { get; set; } - public AppUserDTO Manager { get; set; } + public AppUserDTORead Manager { get; set; } - public List Users { get; set; } = new List(); + public List Users { get; set; } = new List(); - public List Tickets { get; set; } = new List(); + public List Tickets { get; set; } = new List(); public List Activities { get; set; } = new List(); diff --git a/DTOs/Project/ProjectDTORead.cs b/DTOs/Project/ProjectDTORead.cs new file mode 100644 index 0000000..3d5e0ab --- /dev/null +++ b/DTOs/Project/ProjectDTORead.cs @@ -0,0 +1,38 @@ +using System; +using System.Linq; +using System.Collections.Generic; +using TicketManager.Models; + +namespace TicketManager.DTO +{ + public class ProjectDTORead + { + public ProjectDTORead(Project project) + { + Id = project.Id; + Title = project.Title; + Description = project.Description; + CreationDate = project.CreationDate; + EndingDate = project.EndingDate; + Progression = project.Progression; + Status = project.Status.ToString(); + // Manager = project.Manager != null ? new AppUserDTORead(project.Manager) : null; + } + + public int Id { get; set; } + + public string Title { get; set; } + + public string Description { get; set; } + + public DateTime CreationDate { get; private set; } = DateTime.Now; + + public DateTime EndingDate { get; set; } + + public decimal Progression { get; set; } + + public string Status { get; set; } + + public AppUserDTORead Manager { get; set; } + } +} \ No newline at end of file diff --git a/DTOs/TicketDTO.cs b/DTOs/Tickets/TicketDTO.cs similarity index 84% rename from DTOs/TicketDTO.cs rename to DTOs/Tickets/TicketDTO.cs index f1aa724..3c90f84 100644 --- a/DTOs/TicketDTO.cs +++ b/DTOs/Tickets/TicketDTO.cs @@ -20,11 +20,11 @@ namespace TicketManager.DTO Difficulty = ticket.Difficulty.ToString(); Category = ticket.Category.ToString(); CreatorId = ticket.CreatorId; - Project = new ProjectDTO(ticket.Project); + // Project = new ProjectDTORead(ticket.Project); Notes = ticket.Notes; Activities = ticket.Activities; Files = ticket.Files; - Users = ticket.GetAssignees().Select(u => new AppUserDTO(u)).ToList(); + // Users = ticket.GetAssignees().Select(u => new AppUserDTORead(u)).ToList(); } public int Id { get; set; } @@ -49,7 +49,7 @@ namespace TicketManager.DTO public Guid CreatorId { get; set; } - public ProjectDTO Project { get; set; } + public ProjectDTORead Project { get; set; } public List Notes { get; set; } = new List(); @@ -57,6 +57,6 @@ namespace TicketManager.DTO public List Files { get; set; } = new List(); - public List Users { get; set; } = new List(); + public List Users { get; set; } = new List(); } } \ No newline at end of file diff --git a/DTOs/Tickets/TicketDTORead.cs b/DTOs/Tickets/TicketDTORead.cs new file mode 100644 index 0000000..edf131d --- /dev/null +++ b/DTOs/Tickets/TicketDTORead.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using TicketManager.Models; + +namespace TicketManager.DTO +{ + public class TicketDTORead + { + public TicketDTORead(Ticket ticket) + { + Id = ticket.Id; + Title = ticket.Title; + Description = ticket.Description; + CreationDate = ticket.CreationDate; + EndingDate = ticket.EndingDate; + Status = ticket.Status.ToString(); + Impact = ticket.Impact.ToString(); + Difficulty = ticket.Difficulty.ToString(); + Category = ticket.Category.ToString(); + CreatorId = ticket.CreatorId; + Notes = ticket.Notes; + Files = ticket.Files; + } + + public int Id { get; set; } + + public string Title { get; set; } + + public string Description { get; set; } + + [DataType(DataType.Date)] + public DateTime CreationDate { get; private set; } + + [DataType(DataType.Date)] + public DateTime EndingDate { get; set; } + + public string Status { get; set; } + + public string Impact { get; set; } + + public string Difficulty { get; set; } + + public string Category { get; set; } + + public Guid CreatorId { get; set; } + public List Notes { get; set; } = new List(); + + public List Files { get; set; } = new List(); + } +} \ No newline at end of file diff --git a/Models/Activity.cs b/Models/Activity.cs index dcad951..549adf2 100644 --- a/Models/Activity.cs +++ b/Models/Activity.cs @@ -6,7 +6,7 @@ namespace TicketManager.Models { public int Id { get; set; } public string Description { get; set; } - public DateTime UpdateDate { get; set; } = DateTime.Now; + public DateTime UpdateDate { get; private set; } = DateTime.Now; public ActivityType ActivityType { get; set; } = ActivityType.Undefined; public AppUser User { get; set; } public int UserId { get; set; } diff --git a/Models/Interfaces/ITask.cs b/Models/Interfaces/ITask.cs index 8f83813..dccc923 100644 --- a/Models/Interfaces/ITask.cs +++ b/Models/Interfaces/ITask.cs @@ -20,7 +20,6 @@ namespace TicketManager.Models Description = description, ActivityType = ActivityType.Undefined, // User = user, - UpdateDate = DateTime.Now }; Activities.Add(Activity); } diff --git a/Models/Note.cs b/Models/Note.cs index 84d5c05..1dc7578 100644 --- a/Models/Note.cs +++ b/Models/Note.cs @@ -8,7 +8,7 @@ namespace TicketManager.Models public int Id { get; set; } public string Title { get; set; } public string Description { get; set; } - public DateTime Created_at { get; set; } = DateTime.Now; + public DateTime CreationDate { get; private set; } = DateTime.Now; public Ticket Ticket { get; set; } } diff --git a/Models/Project.cs b/Models/Project.cs index 6ed7dd1..48cc364 100644 --- a/Models/Project.cs +++ b/Models/Project.cs @@ -36,25 +36,7 @@ namespace TicketManager.Models Where(t => t.Status == Status.Done).Count() / this.Tickets.Count() * 100; } - // private set - // { - // _progression = - // } } - // public decimal Progression - // { - // get - // { - // return _progression; - // } - // private set - // { - // _progression = Tickets.Count() == 0 ? 0 : - // (decimal)this.Tickets. - // Where(t => t.Status == Status.Done).Count() - // / this.Tickets.Count() * 100; - // } - // } [Display(Name = "Project Status")] public Status Status { get; set; } = Status.ToDo; @@ -95,10 +77,6 @@ namespace TicketManager.Models public void RemoveMembers(List membersToRemove) { this.Assignments.RemoveAll(a => membersToRemove.Contains(a.User)); - - // membersToRemove.ForEach( - // m => m.Assignments.RemoveAll(a => (a.Project == this)) - // ); } public void SetMembers(List projectMembers) diff --git a/Models/Ticket.cs b/Models/Ticket.cs index 233d497..3ebeefb 100644 --- a/Models/Ticket.cs +++ b/Models/Ticket.cs @@ -28,19 +28,15 @@ namespace TicketManager.Models public Impact Impact { get; set; } = Impact.Undefined; public Difficulty Difficulty { get; set; } = Difficulty.Undefined; public Category Category { get; set; } = Category.Undefined; - - // [Display(Name = "Created By")] - // public AppUser Creator { get; set; } public Guid CreatorId { get; set; } [Display(Name = "Project")] public Project Project { get; set; } - // public int ProjectId { get; set; } - public List Notes = new List(); + public List Notes { get; set; } = new List(); - public List Activities = new List(); + public List Activities { get; set; } = new List(); - public List Files = new List(); + public List Files { get; set; } = new List(); // Methods public List GetAssignees() diff --git a/README.md b/README.md index bd72ac2..1af3ca6 100644 --- a/README.md +++ b/README.md @@ -48,4 +48,4 @@ - [ ] error page redirect when offline. - [ ] ticket/files/activities list placeholders when empty - [ ] think about public/private DTO's constructor, getters and setters -- [ ] write dtos without circular dependencies +- [x] write dtos without circular dependencies diff --git a/Scripts/apiQueries.sh b/Scripts/apiQueries.sh index b904838..7802a36 100755 --- a/Scripts/apiQueries.sh +++ b/Scripts/apiQueries.sh @@ -1,2 +1,10 @@ -curl --insecure https://localhost:5001/api/v1/ -curl --insecure https://localhost:5001/api/v1/projects/1/ | json_pp > Scripts/response.http \ No newline at end of file +ROOT=https://localhost:5001/api/v1 +FILE=Scripts/response.json + +rm $FILE +date >> $FILE + +URL=$ROOT/tickets/ +cat $URL >> $FILE + +curl --insecure $URL | json_pp >> $FILE \ No newline at end of file diff --git a/Scripts/response.http b/Scripts/response.http deleted file mode 100644 index e69de29..0000000