public bool UpdateBoardPosition(int userId, BoardModelWithPosition board) { using IDbConnection db = new SqlConnection(_connectionString); Debug.Assert(board.Id != null, "board.Id != null"); if (!IsAllowed(db, userId, (int)board.Id)) { return(false); } int maxBoardPosition = GetBoardsCount(db, userId) - 1; if (board.Position < 0 || board.Position > maxBoardPosition) { return(false); } return(db.Query( @"DECLARE @OldBoardPosition INT; SET @OldBoardPosition = (SELECT Position FROM BoardTable WHERE Id = @boardId); UPDATE BoardTable SET Position = B.Position - 1 FROM BoardTable AS B JOIN UserTable AS U ON U.Id = B.UserId WHERE U.Id = @userId AND B.Position > @OldBoardPosition; UPDATE BoardTable SET Position = B.Position + 1 FROM BoardTable AS B JOIN UserTable AS U ON U.Id = B.UserId WHERE U.Id = @userId AND B.Position >= @newPosition; UPDATE BoardTable SET Position = @newPosition OUTPUT DELETED.Id WHERE Id = @boardId;", new { boardId = board.Id, userId, newPosition = board.Position }).Any()); }
public ActionResult MoveBoard(BoardModelWithPosition board) { int?userId = AuthHelper.GetUserId(HttpContext); if (board.Id == null) { return(BadRequest()); } if (userId == null) { return(Unauthorized()); } bool result = _boardRepository.UpdateBoardPosition((int)userId, board); if (result) { return(Ok()); } return(StatusCode(403)); }