public async Task <CardDTO> ChangeCardAsync(CardDTO cardDTO) { var card = db.Cards.FirstOrDefault(c => c.Id == cardDTO.Id); if (card == null) { throw new Exception("Card is not exist!"); } card.CardsListId = cardDTO.CardsListId; card.ItemNumber = cardDTO.ItemNumber; card.Title = cardDTO.Title; card.Description = cardDTO.Description; card.Start = cardDTO.Start; card.End = cardDTO.End; card.Duration = cardDTO.Duration; if (cardDTO.Discriminator == "PlanCard") { PlanCard planCard = ((PlanCard)card); if (planCard.IsDone != (planCard.DoneUserId != null)) { throw new Exception("Error!"); } planCard.IsDone = ((PlanCardDTO)cardDTO).IsDone; planCard.DoneUserId = ((PlanCardDTO)cardDTO).DoneUserId; } if (cardDTO.Discriminator == "HabitCard") { ((HabitCard)card).DoneCounter = ((HabitCardDTO)cardDTO).DoneCounter; ((HabitCard)card).NotDoneCounter = ((HabitCardDTO)cardDTO).NotDoneCounter; } await db.SaveChangesAsync(); return(db.Cards.FirstOrDefault(c => c.Id == cardDTO.Id).ToCardDTO()); }
public static Card ToCard(this CardDTO cardDTO) { Card newCard = new Card { ItemNumber = cardDTO.ItemNumber, Title = cardDTO.Title, Description = cardDTO.Description, Start = cardDTO.Start, End = cardDTO.End, Duration = cardDTO?.Duration, CardsListId = cardDTO.CardsListId, Complexity = cardDTO?.Complexity }; if (cardDTO.Discriminator == "PlanCard") { PlanCard planCard = (PlanCard)newCard; planCard.IsDone = ((PlanCardDTO)cardDTO).IsDone; planCard.DoneUserId = ((PlanCardDTO)cardDTO).DoneUserId; return(planCard); } else { HabitCard habitCard = (HabitCard)newCard; habitCard.DoneCounter = ((HabitCardDTO)cardDTO).DoneCounter; habitCard.NotDoneCounter = ((HabitCardDTO)cardDTO).NotDoneCounter; return(habitCard); } }
public async Task <CardDTO> CreateCardAsync(CardDTO cardDTO) { if (cardDTO.Discriminator != "PlanCard" && cardDTO.Discriminator != "HabitCard") { throw new Exception("Card type is not exist!"); } if (cardDTO.Discriminator != "PlanCard") { PlanCard planCard = (PlanCard)cardDTO.ToCard(); db.PlanCards.Add(planCard); await db.SaveChangesAsync(); return(db.PlanCards.FirstOrDefault(pc => pc.Id == planCard.Id).ToCardDTO()); } if (cardDTO.Discriminator != "HabitCard") { HabitCard habitCard = (HabitCard)cardDTO.ToCard(); db.HabitCards.Add(habitCard); await db.SaveChangesAsync(); return(db.HabitCards.FirstOrDefault(pc => pc.Id == habitCard.Id).ToCardDTO()); } return(null); }
private static int Comparator(PlanCard first, PlanCard second) { return(first.End.CompareTo(second.End)); }