wrote finer DTOs to avoid circular dependencies

This commit is contained in:
Ruidy Nemausat 2020-02-27 10:10:05 +01:00
parent deb3492475
commit 2b7afa9c09
16 changed files with 166 additions and 50 deletions

1
.gitignore vendored
View file

@ -9,6 +9,7 @@ Data/UnitOfWork.cs
Data/*Repository.cs Data/*Repository.cs
Migrations/ Migrations/
Properties/ Properties/
Scripts/response.json
Tests/TicketManager.Tests/UnitTests/ControllersTests/ControllerTests.cs Tests/TicketManager.Tests/UnitTests/ControllersTests/ControllerTests.cs
Tests/TicketManager.Tests/UnitTests/ControllersTests/SeedDb.cs Tests/TicketManager.Tests/UnitTests/ControllersTests/SeedDb.cs

View file

@ -10,7 +10,7 @@ using TicketManager.Models;
namespace TicketManager.Controllers namespace TicketManager.Controllers
{ {
[Authorize] // [Authorize]
[Route("api/v1/[controller]")] [Route("api/v1/[controller]")]
[ApiController] [ApiController]
public class TicketsController : ControllerBase public class TicketsController : ControllerBase

View file

@ -19,8 +19,8 @@ namespace TicketManager.DTO
CreationDate = user.CreationDate; CreationDate = user.CreationDate;
Picture = user.Picture; Picture = user.Picture;
Activities = user.Activities; Activities = user.Activities;
Projects = user.GetProjects().Select(u => new ProjectDTO(u)).ToList(); Projects = user.GetProjects().Select(u => new ProjectDTORead(u)).ToList();
Tickets = user.GetTickets().Select(u => new TicketDTO(u)).ToList(); Tickets = user.GetTickets().Select(u => new TicketDTORead(u)).ToList();
} }
public Guid Id { get; set; } public Guid Id { get; set; }
@ -46,8 +46,8 @@ namespace TicketManager.DTO
public List<Activity> Activities { get; set; } = new List<Activity>(); public List<Activity> Activities { get; set; } = new List<Activity>();
public List<ProjectDTO> Projects { get; set; } = new List<ProjectDTO>(); public List<ProjectDTORead> Projects { get; set; } = new List<ProjectDTORead>();
public List<TicketDTO> Tickets { get; set; } = new List<TicketDTO>(); public List<TicketDTORead> Tickets { get; set; } = new List<TicketDTORead>();
} }
} }

View file

@ -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; }
}
}

View file

@ -16,9 +16,9 @@ namespace TicketManager.DTO
EndingDate = project.EndingDate; EndingDate = project.EndingDate;
Progression = project.Progression; Progression = project.Progression;
Status = project.Status.ToString(); Status = project.Status.ToString();
// Manager = project.Manager != null ? new AppUserDTO(project.Manager) : null; // Manager = project.Manager != null ? new AppUserDTORead(project.Manager) : null;
Users = project.GetMembers().Select(u => new AppUserDTO(u)).ToList(); Users = project.GetMembers().Select(u => new AppUserDTORead(u)).ToList();
Tickets = project.Tickets.Select(t => new TicketDTO(t)).ToList(); Tickets = project.Tickets.Select(t => new TicketDTORead(t)).ToList();
Activities = project.Activities; Activities = project.Activities;
Files = project.Files; Files = project.Files;
} }
@ -37,11 +37,11 @@ namespace TicketManager.DTO
public string Status { get; set; } public string Status { get; set; }
public AppUserDTO Manager { get; set; } public AppUserDTORead Manager { get; set; }
public List<AppUserDTO> Users { get; set; } = new List<AppUserDTO>(); public List<AppUserDTORead> Users { get; set; } = new List<AppUserDTORead>();
public List<TicketDTO> Tickets { get; set; } = new List<TicketDTO>(); public List<TicketDTORead> Tickets { get; set; } = new List<TicketDTORead>();
public List<Activity> Activities { get; set; } = new List<Activity>(); public List<Activity> Activities { get; set; } = new List<Activity>();

View file

@ -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; }
}
}

View file

@ -20,11 +20,11 @@ namespace TicketManager.DTO
Difficulty = ticket.Difficulty.ToString(); Difficulty = ticket.Difficulty.ToString();
Category = ticket.Category.ToString(); Category = ticket.Category.ToString();
CreatorId = ticket.CreatorId; CreatorId = ticket.CreatorId;
Project = new ProjectDTO(ticket.Project); // Project = new ProjectDTORead(ticket.Project);
Notes = ticket.Notes; Notes = ticket.Notes;
Activities = ticket.Activities; Activities = ticket.Activities;
Files = ticket.Files; 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; } public int Id { get; set; }
@ -49,7 +49,7 @@ namespace TicketManager.DTO
public Guid CreatorId { get; set; } public Guid CreatorId { get; set; }
public ProjectDTO Project { get; set; } public ProjectDTORead Project { get; set; }
public List<Note> Notes { get; set; } = new List<Note>(); public List<Note> Notes { get; set; } = new List<Note>();
@ -57,6 +57,6 @@ namespace TicketManager.DTO
public List<File> Files { get; set; } = new List<File>(); public List<File> Files { get; set; } = new List<File>();
public List<AppUserDTO> Users { get; set; } = new List<AppUserDTO>(); public List<AppUserDTORead> Users { get; set; } = new List<AppUserDTORead>();
} }
} }

View file

@ -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<Note> Notes { get; set; } = new List<Note>();
public List<File> Files { get; set; } = new List<File>();
}
}

View file

@ -6,7 +6,7 @@ namespace TicketManager.Models
{ {
public int Id { get; set; } public int Id { get; set; }
public string Description { 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 ActivityType ActivityType { get; set; } = ActivityType.Undefined;
public AppUser User { get; set; } public AppUser User { get; set; }
public int UserId { get; set; } public int UserId { get; set; }

View file

@ -20,7 +20,6 @@ namespace TicketManager.Models
Description = description, Description = description,
ActivityType = ActivityType.Undefined, ActivityType = ActivityType.Undefined,
// User = user, // User = user,
UpdateDate = DateTime.Now
}; };
Activities.Add(Activity); Activities.Add(Activity);
} }

View file

@ -8,7 +8,7 @@ namespace TicketManager.Models
public int Id { get; set; } public int Id { get; set; }
public string Title { get; set; } public string Title { get; set; }
public string Description { 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; } public Ticket Ticket { get; set; }
} }

View file

@ -36,25 +36,7 @@ namespace TicketManager.Models
Where(t => t.Status == Status.Done).Count() Where(t => t.Status == Status.Done).Count()
/ this.Tickets.Count() * 100; / 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")] [Display(Name = "Project Status")]
public Status Status { get; set; } = Status.ToDo; public Status Status { get; set; } = Status.ToDo;
@ -95,10 +77,6 @@ namespace TicketManager.Models
public void RemoveMembers(List<AppUser> membersToRemove) public void RemoveMembers(List<AppUser> membersToRemove)
{ {
this.Assignments.RemoveAll(a => membersToRemove.Contains(a.User)); this.Assignments.RemoveAll(a => membersToRemove.Contains(a.User));
// membersToRemove.ForEach(
// m => m.Assignments.RemoveAll(a => (a.Project == this))
// );
} }
public void SetMembers(List<AppUser> projectMembers) public void SetMembers(List<AppUser> projectMembers)

View file

@ -28,19 +28,15 @@ namespace TicketManager.Models
public Impact Impact { get; set; } = Impact.Undefined; public Impact Impact { get; set; } = Impact.Undefined;
public Difficulty Difficulty { get; set; } = Difficulty.Undefined; public Difficulty Difficulty { get; set; } = Difficulty.Undefined;
public Category Category { get; set; } = Category.Undefined; public Category Category { get; set; } = Category.Undefined;
// [Display(Name = "Created By")]
// public AppUser Creator { get; set; }
public Guid CreatorId { get; set; } public Guid CreatorId { get; set; }
[Display(Name = "Project")] [Display(Name = "Project")]
public Project Project { get; set; } public Project Project { get; set; }
// public int ProjectId { get; set; } public List<Note> Notes { get; set; } = new List<Note>();
public List<Note> Notes = new List<Note>();
public List<Activity> Activities = new List<Activity>(); public List<Activity> Activities { get; set; } = new List<Activity>();
public List<File> Files = new List<File>(); public List<File> Files { get; set; } = new List<File>();
// Methods // Methods
public List<AppUser> GetAssignees() public List<AppUser> GetAssignees()

View file

@ -48,4 +48,4 @@
- [ ] error page redirect when offline. - [ ] error page redirect when offline.
- [ ] ticket/files/activities list placeholders when empty - [ ] ticket/files/activities list placeholders when empty
- [ ] think about public/private DTO's constructor, getters and setters - [ ] think about public/private DTO's constructor, getters and setters
- [ ] write dtos without circular dependencies - [x] write dtos without circular dependencies

View file

@ -1,2 +1,10 @@
curl --insecure https://localhost:5001/api/v1/ ROOT=https://localhost:5001/api/v1
curl --insecure https://localhost:5001/api/v1/projects/1/ | json_pp > Scripts/response.http FILE=Scripts/response.json
rm $FILE
date >> $FILE
URL=$ROOT/tickets/
cat $URL >> $FILE
curl --insecure $URL | json_pp >> $FILE

View file