public ActionResult Login(string username, string password) { UserResponse response = new UserResponse(); try { var encodePassword = Utils.EncodePassword(password.Trim()); var user = db.User.FirstOrDefault(u => u.UserName == username.Trim() && u.Password == encodePassword && u.Status == (byte)StatusType.Active); if (user == null) { response.Message = "Username or password incorrect!"; return(Unauthorized(response)); } else { response = new UserResponse() { CreatedDate = user.CreatedDate, FullName = user.FullName, Status = user.Status, Token = ClaimPrincipal.GenerateToken(user.UserName), UserId = user.Id, UserName = user.UserName }; return(Ok(response)); } } catch (Exception ex) { return(StatusCode(500, ex.Message)); } }
public ActionResult DeleteUser(long userId) { BaseResponse response = new BaseResponse(); try { var token = new ClaimPrincipal(HttpContext.User); User user = db.User.FirstOrDefault(u => u.UserName == token.NameIdentifier); if (user == null) { response.Message = "The user not found!"; return(NotFound(response)); } if (user.Id != userId) { response.Message = "Unauthorized"; return(Unauthorized(response)); } //Delete user user.Status = (byte)StatusType.Passive; user.UpdatedDate = Utils.GetUnixTimeNow(); user.ModifierBy = user.UserName; db.SaveChanges(); response.Message = "The user deleted!"; return(Ok(response)); } catch (Exception ex) { return(StatusCode(500, ex.Message)); } }
public ActionResult GetUser() { try { var token = new ClaimPrincipal(HttpContext.User); UserResponse response = new UserResponse(); User user = db.User.FirstOrDefault(u => u.UserName == token.NameIdentifier); if (user == null) { response.Message = "The user not found!"; return(NotFound(response)); } response = new UserResponse() { CreatedDate = user.CreatedDate, FullName = user.FullName, Status = user.Status, UserId = user.Id, UserName = user.UserName }; return(Ok(response)); } catch (Exception ex) { return(StatusCode(500, ex.Message)); } }
public ActionResult AddMultipleList([FromBody] List[] listArray) { try { var token = new ClaimPrincipal(HttpContext.User); UserListResponse response = new UserListResponse(); User user = db.User.FirstOrDefault(u => u.UserName == token.NameIdentifier); if (user == null) { response.Message = "The user not found!"; return(NotFound(response)); } foreach (var list in listArray) { //Create list var newList = new List() { CreatedDate = Utils.GetUnixTimeNow(), Description = list.Description, EndsAt = list.EndsAt, ModifierBy = user.UserName, OwnerBy = user.UserName, Priority = list.Priority, StartsAt = list.StartsAt, Status = (byte)StatusType.Active, Title = list.Title, Type = list.Type, UpdatedDate = Utils.GetUnixTimeNow() }; db.List.Add(newList); db.SaveChanges(); //Create user list var userList = new UserList() { ListId = newList.Id, ModifierBy = user.UserName, OwnerBy = user.OwnerBy, Status = (byte)StatusType.Active, UpdatedDate = Utils.GetUnixTimeNow(), CreatedDate = Utils.GetUnixTimeNow(), UserId = user.Id }; db.UserList.Add(userList); db.SaveChanges(); } response = ListService.GetUserList(db, user.Id); return(Ok(response)); } catch (Exception ex) { return(StatusCode(500, ex.Message)); } }
public ActionResult DeleteListType(int typeId) { try { var token = new ClaimPrincipal(HttpContext.User); BaseResponse response = new BaseResponse(); User user = db.User.FirstOrDefault(u => u.UserName == token.NameIdentifier); if (user == null) { response.Message = "The user not found!"; return(NotFound(response)); } //Check user list var userListType = db.UserListType.FirstOrDefault(ul => ul.UserId == user.Id && ul.ListTypeId == typeId); if (userListType == null) { response.Message = "The user list type not found!"; return(Unauthorized(response)); } //Delete user list userListType.Status = (byte)StatusType.Passive; userListType.UpdatedDate = Utils.GetUnixTimeNow(); userListType.ModifierBy = user.UserName; //Check the list var currentListType = db.ListType.Find(typeId); if (currentListType == null) { response.Message = "The list type not found!"; return(NotFound(response)); } //Check list type in active list var activeList = db.List.Where(l => l.Type == currentListType.Id && l.Status == (byte)StatusType.Active).ToList(); if (activeList.Count > 0) { response.Message = "This list type using for active list. Firstly, delete the list and then try delete the list type."; return(StatusCode(414, response)); } //Delete current list currentListType.Status = (byte)StatusType.Passive; currentListType.UpdatedDate = Utils.GetUnixTimeNow(); currentListType.ModifierBy = user.UserName; db.SaveChanges(); response.Message = "The list type deleted successfully!"; return(Ok(response)); } catch (Exception ex) { return(StatusCode(500, ex.Message)); } }
public ActionResult UpdateUser([FromBody] UpdateUserRequest request) { try { UserResponse response = new UserResponse(); var token = new ClaimPrincipal(HttpContext.User); //Check request body object if (request == null) { response.Message = "Request body can not be empty!"; return(BadRequest(response)); } //Check authorization if (token.NameIdentifier != request.Username) { response.Message = "Request body can not be empty!"; return(Unauthorized(response)); } User user = db.User.FirstOrDefault(u => u.UserName == token.NameIdentifier); if (user == null) { response.Message = "The user not found!"; return(NotFound(response)); } user.ModifierBy = user.UserName; user.UpdatedDate = Utils.GetUnixTimeNow(); user.FullName = !string.IsNullOrEmpty(request.FullName) ? request.FullName : user.FullName; user.Password = !string.IsNullOrEmpty(request.Password) ? Utils.EncodePassword(request.Password) : user.Password; db.SaveChanges(); response = new UserResponse() { CreatedDate = user.CreatedDate, FullName = user.FullName, Message = "The user updated!", Status = user.Status, UserId = user.Id, UserName = user.UserName }; return(Ok(response)); } catch (Exception ex) { return(StatusCode(500, ex.Message)); } }
public ActionResult AddTypeToList(long listId, int typeId) { try { var token = new ClaimPrincipal(HttpContext.User); UserListResponse response = new UserListResponse(); User user = db.User.FirstOrDefault(u => u.UserName == token.NameIdentifier); if (user == null) { response.Message = "The user not found!"; return(NotFound(response)); } //Check user list var userList = db.UserList.FirstOrDefault(ul => ul.UserId == user.Id && ul.ListId == listId); if (userList == null) { response.Message = "Unauthorized!"; return(Unauthorized(response)); } //Check the list var currentList = db.List.FirstOrDefault(l => l.Id == listId); if (currentList == null) { response.Message = "The list not found!"; return(NotFound(response)); } //Get list type var listType = db.ListType.FirstOrDefault(l => l.Id == typeId && l.Status == (byte)StatusType.Active); if (listType == null) { response.Message = "The list type not found!"; return(NotFound(response)); } currentList.ModifierBy = user.UserName; currentList.UpdatedDate = Utils.GetUnixTimeNow(); currentList.Type = listType.Id; db.SaveChanges(); response = ListService.GetUserList(db, user.Id); return(Ok(response)); } catch (Exception ex) { return(StatusCode(500, ex.Message)); } }
public ActionResult AddListType([FromBody] ListType listType) { try { var token = new ClaimPrincipal(HttpContext.User); BaseResponse response = new BaseResponse(); User user = db.User.FirstOrDefault(u => u.UserName == token.NameIdentifier); if (user == null) { response.Message = "The user not found!"; return(NotFound(response)); } //Create list type var newListType = new ListType() { CreatedDate = Utils.GetUnixTimeNow(), Description = listType.Description, ModifierBy = user.UserName, OwnerBy = user.UserName, Status = (byte)StatusType.Active, UpdatedDate = Utils.GetUnixTimeNow(), Name = listType.Name }; db.ListType.Add(newListType); db.SaveChanges(); //Create user list type var userListType = new UserListType() { ListTypeId = newListType.Id, ModifierBy = user.UserName, OwnerBy = user.OwnerBy, Status = (byte)StatusType.Active, UpdatedDate = Utils.GetUnixTimeNow(), CreatedDate = Utils.GetUnixTimeNow(), UserId = user.Id }; db.UserListType.Add(userListType); db.SaveChanges(); response.Message = "The list type added successfully"; return(Ok(response)); } catch (Exception ex) { return(StatusCode(500, ex.Message)); } }
public ActionResult DeleteList(long listId) { try { var token = new ClaimPrincipal(HttpContext.User); BaseResponse response = new BaseResponse(); User user = db.User.FirstOrDefault(u => u.UserName == token.NameIdentifier); if (user == null) { response.Message = "The user not found!"; return(NotFound(response)); } //Check user list var userList = db.UserList.FirstOrDefault(ul => ul.UserId == user.Id && ul.ListId == listId); if (userList == null) { response.Message = "Unauthorized!"; return(Unauthorized(response)); } //Delete user list userList.Status = 0; userList.UpdatedDate = Utils.GetUnixTimeNow(); userList.ModifierBy = user.UserName; //Check the list var currentList = db.List.Find(listId); if (currentList == null) { response.Message = "The list not found!"; return(NotFound(response)); } //Delete current list currentList.Status = (byte)StatusType.Passive; currentList.UpdatedDate = Utils.GetUnixTimeNow(); currentList.ModifierBy = user.UserName; db.SaveChanges(); response.Message = "The list deleted! successfully"; return(Ok(response)); } catch (Exception ex) { return(StatusCode(500, ex.Message)); } }
public ActionResult UpdateList([FromBody] List list) { try { var token = new ClaimPrincipal(HttpContext.User); BaseResponse response = new BaseResponse(); User user = db.User.FirstOrDefault(u => u.UserName == token.NameIdentifier); if (user == null) { response.Message = "The user not found!"; return(NotFound(response)); } //Check user list var userList = db.UserList.FirstOrDefault(ul => ul.UserId == user.Id && ul.ListId == list.Id); if (userList == null) { return(Unauthorized("Unauthorized!")); } //Check the list var currentList = db.List.Find(list.Id); if (currentList == null) { response.Message = "The list not found!"; return(NotFound(response)); } currentList.ModifierBy = user.UserName; currentList.UpdatedDate = Utils.GetUnixTimeNow(); currentList.Description = !string.IsNullOrEmpty(list.Description) ? list.Description : currentList.Description; currentList.EndsAt = list.EndsAt != null ? list.EndsAt : currentList.EndsAt; currentList.Priority = list.Priority != null ? list.Priority : currentList.Priority; currentList.StartsAt = list.StartsAt != 0 ? list.StartsAt : currentList.StartsAt; currentList.Title = !string.IsNullOrEmpty(list.Title) ? list.Title : currentList.Title; currentList.Type = list.Type != 0 ? list.Type : currentList.Type; db.SaveChanges(); response.Message = "The list updated successfully"; return(Ok(response)); } catch (Exception ex) { return(StatusCode(500, ex.Message)); } }
public ActionResult GetUserList() { try { var token = new ClaimPrincipal(HttpContext.User); UserListResponse response = new UserListResponse(); User user = db.User.FirstOrDefault(u => u.UserName == token.NameIdentifier); if (user == null) { response.Message = "The user not found!"; return(NotFound(response)); } return(Ok(ListService.GetUserList(db, user.Id))); } catch (Exception ex) { return(StatusCode(500, ex.Message)); } }
public ActionResult GetUserListType() { try { var token = new ClaimPrincipal(HttpContext.User); UserListTypeResponse response = new UserListTypeResponse(); User user = db.User.FirstOrDefault(u => u.UserName == token.NameIdentifier); if (user == null) { response.Message = "The user not found!"; return(NotFound(response)); } //Get user type list var type = from ut in db.UserListType join lt in db.ListType on ut.ListTypeId equals lt.Id where ut.UserId == user.Id && ut.Status == (byte)StatusType.Active && lt.Status == (byte)StatusType.Active select lt; response = new UserListTypeResponse() { List = type.ToList(), UserId = user.Id }; return(Ok(response)); } catch (Exception ex) { return(StatusCode(500, ex.Message)); } }