public async Task <ActionResult <UserResponse> > UpdateProfile([FromBody] UpdateProfileRequest request) { var login = User.Identity.Name; var users = await db.Users.AsNoTracking().ToListAsync(); var user = users.Find(x => x.Login == login); if (user == null) { return(StatusCode(400, "There is not the user")); } var updatedUser = new User { Id = user.Id, Login = !string.IsNullOrEmpty(request.Login) ? request.Login : user.Login, Password = !string.IsNullOrEmpty(request.Password) ? request.Password : user.Password, Name = request.Name, Surname = request.Surname }; db.Update(updatedUser); await db.SaveChangesAsync(); return(new UserResponse { Name = updatedUser.Name, Surname = updatedUser.Surname }); }
public async Task <ActionResult <ErrorHistoryResponse> > Update(int id, [FromBody] ErrorHistoryItemRequest request) { var user = await db.Users.FirstOrDefaultAsync(x => x.Login == User.Identity.Name); var error = await db.Errors.FindAsync(id); if (error == null) { return(BadRequest("The is no error")); } if (error.State >= (Error.StateEnum)request.Action) { return(BadRequest($"State '{(Error.StateEnum)request.Action}' cannot become after '{error.State}'")); } var historyItem = new ErrorHistory { Date = DateTime.Now, Action = request.Action, Comment = request.Comment, UserId = user.Id, ErrorId = id }; error.State = (Error.StateEnum)historyItem.Action; db.ErrorHistory.Add(historyItem); db.Update(error); await db.SaveChangesAsync(); return(new ErrorHistoryResponse { Id = historyItem.Id, Date = historyItem.Date, Action = historyItem.Action, Comment = historyItem.Comment, User = new UserResponse { Name = user.Name, Surname = user.Surname } }); }