public async Task <IActionResult> UpdateTask([FromRoute] int id, [FromBody] Todo todo) { if (string.IsNullOrEmpty(_cookieService.getCookieValue(HttpContext))) { return(Unauthorized()); } else if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != todo.id) { return(BadRequest()); } var originalTodo = await _context.Todo.Where(t => t.id == id).AsNoTracking().Include(t => t.user).FirstOrDefaultAsync(); var localUser = await _context.User.Where(u => u.authToken == _cookieService.getCookieValue(HttpContext)).FirstOrDefaultAsync(); if (localUser == null) { return(Unauthorized()); } else { if (localUser.authToken != originalTodo.user.authToken) { return(Unauthorized()); } } todo.user = localUser; _context.Entry(todo).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!TodoExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> UpdateUser([FromRoute] int id, [FromBody] User user) { if (_cookieService.getCookieValue(HttpContext) == "") { return(Unauthorized()); } else if (!await checkAuthorisation(_cookieService.getCookieValue(HttpContext))) { return(Unauthorized()); } else if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != user.id) { return(BadRequest()); } else if (user.emailAddress == null && user.emailAddress == "") { return(BadRequest()); } else if (string.IsNullOrEmpty(user.username) || string.IsNullOrWhiteSpace(user.username)) { return(BadRequest()); } if (string.IsNullOrEmpty(user.password) || string.IsNullOrWhiteSpace(user.password)) { User localUser = await _context.User.AsNoTracking().Where(u => u.id == user.id).FirstOrDefaultAsync(); user.password = localUser.password; } else { user.password = saltedHashedPassword(user.password); } user.authToken = (await _context.User.AsNoTracking().Where(u => u.id == user.id).FirstOrDefaultAsync()).authToken; _context.Entry(user).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!UserExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }