diff --git a/Controllers/UsersController.cs b/Controllers/AppUsersController.cs similarity index 53% rename from Controllers/UsersController.cs rename to Controllers/AppUsersController.cs index 32ca0f3..ff45085 100644 --- a/Controllers/UsersController.cs +++ b/Controllers/AppUsersController.cs @@ -23,22 +23,16 @@ namespace TicketManager.Controllers // GET: api/Users [HttpGet] - public async Task>> GetUsers() + public async Task>> GetUsers() { - return await _context.Users - .Include(p => p.Assignments) - .Include(p => p.Edits) - .ToListAsync(); + return await getAllAppUsersAsync(); } // GET: api/Users/5 [HttpGet("{id}")] - public async Task> GetUser(Guid id) + public async Task> GetUser(Guid id) { - var user = await _context.Users - .Include(p => p.Assignments) - .Include(p => p.Edits) - .FirstOrDefaultAsync(p => p.Id == id); + var user = await getAppUserByIdAsync(id); if (user == null) { @@ -52,7 +46,7 @@ namespace TicketManager.Controllers // To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see https://aka.ms/RazorPagesCRUD. [HttpPut("{id}")] - public async Task PutUser(Guid id, User user) + public async Task PutUser(Guid id, AppUser user) { if (id != user.Id) { @@ -84,9 +78,9 @@ namespace TicketManager.Controllers // To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see https://aka.ms/RazorPagesCRUD. [HttpPost] - public async Task> PostUser(User user) + public async Task> PostUser(AppUser user) { - _context.Users.Add(user); + _context.AppUsers.Add(user); await _context.SaveChangesAsync(); return CreatedAtAction("GetUser", new { id = user.Id }, user); @@ -94,23 +88,64 @@ namespace TicketManager.Controllers // DELETE: api/Users/5 [HttpDelete("{id}")] - public async Task> DeleteUser(int id) + public async Task> DeleteUser(int id) { - var user = await _context.Users.FindAsync(id); + var user = await _context.AppUsers.FindAsync(id); if (user == null) { return NotFound(); } - _context.Users.Remove(user); + _context.AppUsers.Remove(user); await _context.SaveChangesAsync(); return user; } + [HttpGet("{id}/projects")] + public async Task>> GetAppUserProjects(Guid id) + { + AppUser user = await getAppUserByIdAsync(id); + if (user == null) + { + return BadRequest(); + } + return user.GetProjects(); + } + + [HttpGet("{id}/tickets/")] + public async Task>> GetAppUserTickets(Guid id) + { + AppUser user = await getAppUserByIdAsync(id); + if (user == null) + { + return BadRequest(); + } + return user.GetTickets(); + } + private bool UserExists(Guid id) { - return _context.Users.Any(e => e.Id == id); + return _context.AppUsers.Any(e => e.Id == id); + } + + private IQueryable appUserQuery() + { + return _context.AppUsers + .Include(p => p.Assignments) + .ThenInclude(a => a.Project) + .ThenInclude(p => p.Tickets) + .Include(p => p.Edits); + } + + private async Task>> getAllAppUsersAsync() + { + return await appUserQuery().ToListAsync(); + } + + private async Task getAppUserByIdAsync(Guid id) + { + return await appUserQuery().FirstOrDefaultAsync(a => a.Id == id); } } } diff --git a/Controllers/ProjectsController.cs b/Controllers/ProjectsController.cs index a6b7971..f4b3da6 100644 --- a/Controllers/ProjectsController.cs +++ b/Controllers/ProjectsController.cs @@ -146,14 +146,14 @@ namespace TicketManager.Controllers } [HttpGet("{id}/members")] - public async Task>> GetProjectMembers(int id) + public async Task>> GetProjectMembers(int id) { Project project = await GetProjectByIdAsync(id); return project.GetMembers(); } - [HttpPut("{id}/setMembers")] // test put & post - public async Task> SetProjectMembers(int id, List projectMembers) + [HttpPut("{id}/setMembers")] + public async Task> SetProjectMembers(int id, List projectMembers) { Project project = await GetProjectByIdAsync(id); project.SetMembers(projectMembers); @@ -168,11 +168,11 @@ namespace TicketManager.Controllers "Try again, and if the problem persists, " + "see your system administrator."); } - return project; + return NoContent(); } [HttpPut("{id}/addMembers")] - public async Task> AddMembersToProject(int id, List usersToAdd) + public async Task> AddMembersToProject(int id, List usersToAdd) { if (usersToAdd == null) { @@ -191,11 +191,11 @@ namespace TicketManager.Controllers "Try again, and if the problem persists, " + "see your system administrator."); } - return project; + return NoContent(); } [HttpPut("{id}/removeMembers")] - public async Task> RemoveMembersFromProject(int id, List usersToRemove) + public async Task> RemoveMembersFromProject(int id, List usersToRemove) { Project project = await GetProjectByIdAsync(id); project.RemoveMembers(usersToRemove); @@ -210,7 +210,7 @@ namespace TicketManager.Controllers "Try again, and if the problem persists, " + "see your system administrator."); } - return project; + return NoContent(); } private bool ProjectExists(int id) diff --git a/Data/AppDbContext.cs b/Data/AppDbContext.cs index 92e11be..b249c6b 100644 --- a/Data/AppDbContext.cs +++ b/Data/AppDbContext.cs @@ -9,7 +9,7 @@ namespace TicketManager.Data { } public DbSet Projects { get; set; } - public DbSet Users { get; set; } + public DbSet AppUsers { get; set; } public DbSet Tickets { get; set; } public DbSet Assignments { get; set; } public DbSet Edits { get; set; } diff --git a/Models/AppUser.cs b/Models/AppUser.cs new file mode 100644 index 0000000..216007a --- /dev/null +++ b/Models/AppUser.cs @@ -0,0 +1,70 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; + +namespace TicketManager.Models +{ + public class AppUser + { + public Guid Id { get; set; } + + [Required] + [StringLength(50)] + [Display(Name = "First Name")] + public string FirstName { get; set; } + + [Required] + [StringLength(50)] + [Display(Name = "Last Name")] + public string LastName { get; set; } + + [Display(Name = "Full Name")] + public string FullName => $"{FirstName} {LastName}"; + + [StringLength(200)] + [Display(Name = "Bio")] + public string Presentation { get; set; } + + [DataType(DataType.EmailAddress)] + public string Email { get; set; } + + [DataType(DataType.PhoneNumber)] + public string Phone { get; set; } + + [DataType(DataType.Date)] + [Display(Name = "Member since"), DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}")] + public DateTime Created_at { get; set; } = DateTime.Now; + + // [Display(Name = "Avatar")] + // public byte[] Picture { get; set; } + // public Role Role { get; set; } + + public List Assignments { get; set; } = new List(); + + [Display(Name = "Activity")] + public List Edits { get; set; } = new List(); + + // Methods + public List GetProjects() + { + return Assignments.Select(a => a.Project).ToList(); + } + + // public Project GetProject(int id) + // { + // return Assignments.Single(a => a.Project.Id == id).Project; + // } + + // public List GetProjectMembers(int id) + // { + // return GetProject(id).GetMembers(); + // } + public List GetTickets() + { + List tickets = new List(); + GetProjects().ForEach(p => tickets.Concat(p.Tickets)); + return tickets; + } + } +} \ No newline at end of file diff --git a/Models/Assignment.cs b/Models/Assignment.cs index 42afe79..4d3c496 100644 --- a/Models/Assignment.cs +++ b/Models/Assignment.cs @@ -5,7 +5,7 @@ namespace TicketManager.Models public class Assignment { // public int Id { get; set; } - public User User { get; set; } + public AppUser User { get; set; } public Guid UserId { get; set; } public Project Project { get; set; } public int ProjectId { get; set; } diff --git a/Models/File.cs b/Models/File.cs index 8a7d885..2b5c3b8 100644 --- a/Models/File.cs +++ b/Models/File.cs @@ -11,7 +11,7 @@ namespace TicketManager.Models public int Size { get; set; } public string Format { get; set; } - public User AddedBy { get; set; } + public AppUser AddedBy { get; set; } public int UserId { get; set; } // public ITask AddedTo { get; set; } // public int ITaskId { get; set; } diff --git a/Models/History.cs b/Models/History.cs index 53b4be7..e039343 100644 --- a/Models/History.cs +++ b/Models/History.cs @@ -9,7 +9,7 @@ namespace TicketManager.Models public string Description { get; set; } public DateTime UpdateDate { get; set; } = DateTime.Now; public ActivityType ActivityType { get; set; } = ActivityType.Undefined; - public User User { get; set; } + public AppUser User { get; set; } public int UserId { get; set; } } } \ No newline at end of file diff --git a/Models/Interfaces/ITask.cs b/Models/Interfaces/ITask.cs index 0773186..124b8c0 100644 --- a/Models/Interfaces/ITask.cs +++ b/Models/Interfaces/ITask.cs @@ -12,5 +12,17 @@ namespace TicketManager.Models DateTime CreatedAt { get; } DateTime PlannedEnding { get; set; } List Edits { get; set; } + + public virtual void AddLogEntry(string description)//, User user) + { + History Edit = new History() + { + Description = description, + ActivityType = ActivityType.Undefined, + // User = user, + UpdateDate = DateTime.Now + }; + Edits.Add(Edit); + } } } diff --git a/Models/Project.cs b/Models/Project.cs index 628c740..4ebe976 100644 --- a/Models/Project.cs +++ b/Models/Project.cs @@ -18,12 +18,6 @@ namespace TicketManager.Models [Display(Name = "Short Description")] public string Description { get; set; } - [DataType(DataType.EmailAddress)] - public string Email { get; set; } - - [DataType(DataType.PhoneNumber)] - public string Phone { get; set; } - [DataType(DataType.Date)] [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = false)] public DateTime CreatedAt { get; private set; } = DateTime.Now; @@ -32,16 +26,20 @@ namespace TicketManager.Models [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] public DateTime PlannedEnding { get; set; } + private decimal _progression; [Display(Name = "Progress")] - public float Progression + public decimal Progression { get { - return Tickets.Count() == 0 ? 0 : - (float)this.Tickets. + return _progression; + } + set + { + _progression = Tickets.Count() == 0 ? 0 : + (decimal)this.Tickets. Where(t => t.Status == Status.Done).Count() - / this.Tickets.Count() - * 100; + / this.Tickets.Count() * 100; } } @@ -49,7 +47,7 @@ namespace TicketManager.Models public Status Status { get; set; } = Status.ToDo; [Display(Name = "Project Manager")] - public User Manager { get; set; } + public AppUser Manager { get; set; } public List Assignments { get; set; } = new List(); @@ -60,11 +58,11 @@ namespace TicketManager.Models public List Files { get; set; } = new List(); // Methods - public List GetMembers() + public List GetMembers() { return this.Assignments.Select(a => a.User).ToList(); } - public void AddMembers(List usersToAdd) + public void AddMembers(List usersToAdd) { foreach (var user in usersToAdd) { @@ -76,15 +74,15 @@ namespace TicketManager.Models UserId = user.Id }; this.Assignments.Add(newAssign); - AddLogEntry(" joined the project."); + // AddLogEntry(this, " joined the project."); } } - public void RemoveMembers(List membersToRemove) + public void RemoveMembers(List membersToRemove) { this.Assignments.RemoveAll(a => membersToRemove.Contains(a.User)); } - public void SetMembers(List projectMembers) + public void SetMembers(List projectMembers) { var currentProjectMembers = this.GetMembers(); if (currentProjectMembers != null) @@ -105,7 +103,6 @@ namespace TicketManager.Models this.AddMembers(projectMembers); } - } public int GetMembersCount() => this.GetMembers().Count(); public void GetTicketsCount() => this.Tickets.Count(); @@ -116,16 +113,16 @@ namespace TicketManager.Models this.Status = Status.Done; } - private void AddLogEntry(string description)//, User user) - { - History Edit = new History() - { - Description = description, - ActivityType = ActivityType.Undefined, - // User = user, - UpdateDate = DateTime.Now - }; - this.Edits.Add(Edit); - } + // private void AddLogEntry(string description)//, User user) + // { + // History Edit = new History() + // { + // Description = description, + // ActivityType = ActivityType.Undefined, + // // User = user, + // UpdateDate = DateTime.Now + // }; + // this.Edits.Add(Edit); + // } } } \ No newline at end of file diff --git a/Models/Ticket.cs b/Models/Ticket.cs index 6176197..a423c09 100644 --- a/Models/Ticket.cs +++ b/Models/Ticket.cs @@ -16,7 +16,7 @@ namespace TicketManager.Models public Difficulty Difficulty { get; set; } = Difficulty.Undefined; public Category Category { get; set; } = Category.Undefined; - public User Creator { get; set; } + public AppUser Creator { get; set; } public Guid CreatorId { get; set; } public Project Project { get; set; } public int ProjectId { get; set; } diff --git a/Models/User.cs b/Models/User.cs deleted file mode 100644 index d7ed052..0000000 --- a/Models/User.cs +++ /dev/null @@ -1,44 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace TicketManager.Models -{ - public class User - { - 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; } - public DateTime Created_at { get; set; } = DateTime.Now; - public byte[] Picture { get; set; } - // public Role Role { get; set; } - - private List _assignments; - public List Assignments - { - get - { - return _assignments ?? new List(); - } - set { _assignments = value; } - } - - private List _edits; - public List Edits - { - get - { - return _edits ?? new List(); - } - set { _edits = value; } - } - - // Methods - public void GetProjects() { throw new NotImplementedException("Not Implemented"); } - public void GetProjectMembers() { throw new NotImplementedException("Not Implemented"); } - public void GetTickets() { throw new NotImplementedException("Not Implemented"); } - - - } -} \ No newline at end of file