appuser model & endpoints done

This commit is contained in:
Ruidy Nemausat 2020-02-12 08:13:32 +01:00
parent a46c9aaa0b
commit 734d8e931b
11 changed files with 173 additions and 103 deletions

View file

@ -23,22 +23,16 @@ namespace TicketManager.Controllers
// GET: api/Users
[HttpGet]
public async Task<ActionResult<IEnumerable<User>>> GetUsers()
public async Task<ActionResult<IEnumerable<AppUser>>> 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<ActionResult<User>> GetUser(Guid id)
public async Task<ActionResult<AppUser>> 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<IActionResult> PutUser(Guid id, User user)
public async Task<IActionResult> 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<ActionResult<User>> PostUser(User user)
public async Task<ActionResult<AppUser>> 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<ActionResult<User>> DeleteUser(int id)
public async Task<ActionResult<AppUser>> 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<ActionResult<IEnumerable<Project>>> GetAppUserProjects(Guid id)
{
AppUser user = await getAppUserByIdAsync(id);
if (user == null)
{
return BadRequest();
}
return user.GetProjects();
}
[HttpGet("{id}/tickets/")]
public async Task<ActionResult<IEnumerable<Ticket>>> 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<AppUser> appUserQuery()
{
return _context.AppUsers
.Include(p => p.Assignments)
.ThenInclude(a => a.Project)
.ThenInclude(p => p.Tickets)
.Include(p => p.Edits);
}
private async Task<ActionResult<IEnumerable<AppUser>>> getAllAppUsersAsync()
{
return await appUserQuery().ToListAsync();
}
private async Task<AppUser> getAppUserByIdAsync(Guid id)
{
return await appUserQuery().FirstOrDefaultAsync(a => a.Id == id);
}
}
}

View file

@ -146,14 +146,14 @@ namespace TicketManager.Controllers
}
[HttpGet("{id}/members")]
public async Task<ActionResult<List<User>>> GetProjectMembers(int id)
public async Task<ActionResult<List<AppUser>>> GetProjectMembers(int id)
{
Project project = await GetProjectByIdAsync(id);
return project.GetMembers();
}
[HttpPut("{id}/setMembers")] // test put & post
public async Task<ActionResult<Project>> SetProjectMembers(int id, List<User> projectMembers)
[HttpPut("{id}/setMembers")]
public async Task<ActionResult<Project>> SetProjectMembers(int id, List<AppUser> 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<ActionResult<Project>> AddMembersToProject(int id, List<User> usersToAdd)
public async Task<ActionResult<Project>> AddMembersToProject(int id, List<AppUser> 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<ActionResult<Project>> RemoveMembersFromProject(int id, List<User> usersToRemove)
public async Task<ActionResult<Project>> RemoveMembersFromProject(int id, List<AppUser> 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)

View file

@ -9,7 +9,7 @@ namespace TicketManager.Data
{ }
public DbSet<Project> Projects { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<AppUser> AppUsers { get; set; }
public DbSet<Ticket> Tickets { get; set; }
public DbSet<Assignment> Assignments { get; set; }
public DbSet<History> Edits { get; set; }

70
Models/AppUser.cs Normal file
View file

@ -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<Assignment> Assignments { get; set; } = new List<Assignment>();
[Display(Name = "Activity")]
public List<History> Edits { get; set; } = new List<History>();
// Methods
public List<Project> GetProjects()
{
return Assignments.Select(a => a.Project).ToList();
}
// public Project GetProject(int id)
// {
// return Assignments.Single(a => a.Project.Id == id).Project;
// }
// public List<AppUser> GetProjectMembers(int id)
// {
// return GetProject(id).GetMembers();
// }
public List<Ticket> GetTickets()
{
List<Ticket> tickets = new List<Ticket>();
GetProjects().ForEach(p => tickets.Concat(p.Tickets));
return tickets;
}
}
}

View file

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

View file

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

View file

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

View file

@ -12,5 +12,17 @@ namespace TicketManager.Models
DateTime CreatedAt { get; }
DateTime PlannedEnding { get; set; }
List<History> 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);
}
}
}

View file

@ -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<Assignment> Assignments { get; set; } = new List<Assignment>();
@ -60,11 +58,11 @@ namespace TicketManager.Models
public List<File> Files { get; set; } = new List<File>();
// Methods
public List<User> GetMembers()
public List<AppUser> GetMembers()
{
return this.Assignments.Select(a => a.User).ToList();
}
public void AddMembers(List<User> usersToAdd)
public void AddMembers(List<AppUser> 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<User> membersToRemove)
public void RemoveMembers(List<AppUser> membersToRemove)
{
this.Assignments.RemoveAll(a => membersToRemove.Contains(a.User));
}
public void SetMembers(List<User> projectMembers)
public void SetMembers(List<AppUser> 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);
// }
}
}

View file

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

View file

@ -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<Assignment> _assignments;
public List<Assignment> Assignments
{
get
{
return _assignments ?? new List<Assignment>();
}
set { _assignments = value; }
}
private List<History> _edits;
public List<History> Edits
{
get
{
return _edits ?? new List<History>();
}
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"); }
}
}