public ActionResult UpdateCardPosition(int cardId, int listId, int positionCard, int oldListId) { var updateCardPositionDto = new UpdateCardPositionDto() { ListId = listId, CardId = cardId, PositionCard = positionCard, OldListId = oldListId }; _boardsService.UpdateCardPosition(updateCardPositionDto); return(new HttpStatusCodeResult(HttpStatusCode.OK)); }
public void UpdateCardPosition(UpdateCardPositionDto updateCardPositionDto) { using (var db = new PGSBoardContext()) { if (updateCardPositionDto.ListId == updateCardPositionDto.OldListId) // first case when you sort cart in the same list { var currentListCard = db.Cards.Where(card => card.ListId == updateCardPositionDto.ListId).ToList(); // take whole card from list, just one call to db var cardToUpdate = currentListCard.Single(card => card.Id == updateCardPositionDto.CardId); // take card to Update (card which we drage and drop) var cardChangedAutomatically = // select card which have to change position after our drag and drop move currentListCard.Single(card => card.PositionCardId == updateCardPositionDto.PositionCard); cardChangedAutomatically.PositionCardId = cardChangedAutomatically.PositionCardId > cardToUpdate.PositionCardId // 2 cases: First, if you take card from up to down then position card which have to change will be smaller about -1 ? cardChangedAutomatically.PositionCardId - 1 // Second, if you take card from down to up, then position card which have to change will be larger about +1 : cardChangedAutomatically.PositionCardId + 1; cardToUpdate.PositionCardId = updateCardPositionDto.PositionCard; // update position card which we drag and drop db.SaveChanges(); } else // second case when you sort( drag some card) to another list { var cards = db.Cards.Where(card => card.ListId == updateCardPositionDto.ListId || card.ListId == updateCardPositionDto.OldListId).ToList(); //take card from two lists var cardToUpdate = cards.Single(card => card.Id == updateCardPositionDto.CardId); // take our card which we drag and drop var prevCardsList = cards .Where(card => card.ListId == updateCardPositionDto.OldListId && //take old list, where was our card card.PositionCardId > cardToUpdate.PositionCardId && // take all card which had larger position card.Id != updateCardPositionDto.CardId).ToList(); // don't take card which we drag and drop var nextCardsList = cards .Where(card => card.ListId == updateCardPositionDto.ListId && // take new list, where we drop our card card.PositionCardId >= updateCardPositionDto.PositionCard).ToList(); foreach (var prevCard in prevCardsList) { prevCard.PositionCardId = prevCard.PositionCardId - 1; // old list, reduce position about -1 } foreach (var nextCard in nextCardsList) { nextCard.PositionCardId = nextCard.PositionCardId + 1; // new list increase position card about 1 } cardToUpdate.ListId = updateCardPositionDto.ListId; cardToUpdate.PositionCardId = updateCardPositionDto.PositionCard; //update dropped position card db.SaveChanges(); } } }
public void UpdateCardPosition(UpdateCardPositionDto updateCardPositionDto) { this.boardsRepository.UpdateCardPosition(updateCardPositionDto); }