Beispiel #1
0
        public async Task <IActionResult> Create([Bind("Id,Suit,Value")] Deck deck)
        {
            if (ModelState.IsValid)
            {
                _context.Add(deck);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(deck));
        }
Beispiel #2
0
        /// <summary>
        /// создание колоды
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task <Deck> CreateDeck(CreateDeckRequest request)
        {
            using (var db = new DeckContext())
            {
                var result = new Deck();
                var exist  = db.Decks.FirstOrDefault(x => x.Title.ToLower() == request.Title.ToLower());
                if (exist != null)
                {
                    throw new Exception($"deck with title {request.Title} exist");
                }
                var cardsToDeck = string.Empty;
                if (request.Cards.Any())
                {
                    var selectedCards = request.Cards.Where(x => x.IsEnabled).OrderBy(x => x.SuitTitle).ThenBy(x => x.ValueTitle).ToList();
                    var cardsIds      = selectedCards.Select(x => x.Id).ToList();
                    cardsToDeck = cardsIds.SerializeToJson();
                }
                else
                {
                    cardsToDeck = new List <long>().SerializeToJson();
                }
                db.Decks.Add(new Deck()
                {
                    Title      = request.Title,
                    CardsIds   = cardsToDeck,
                    DateModify = DateTime.UtcNow
                });
                await db.SaveChangesAsync();

                return(result);
            }
        }
Beispiel #3
0
        /// <summary>
        /// удаление карты
        /// </summary>
        /// <param name="id">ид карты</param>
        /// <returns></returns>
        public async Task DeleteCard(long id)
        {
            using (var db = new DeckContext())
            {
                var exist = await db.Cards.FindAsync(id);

                if (exist != null)
                {
                    var decks = db.Decks.ToList();
                    foreach (var deck in decks)
                    {
                        var cardsIds = new JsonSerializer().Deserialize <List <long> >
                                           (new JsonTextReader
                                               (new StringReader(deck.CardsIds)));
                        if (cardsIds.Contains(exist.Id))
                        {
                            cardsIds.Remove(exist.Id);
                            deck.CardsIds = cardsIds.SerializeToJson();
                        }
                    }
                    db.Cards.Remove(exist);
                    await db.SaveChangesAsync();
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// удалить доску
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DeleteDeck(long id)
        {
            using (var db = new DeckContext())
            {
                var exist = await db.Decks.FindAsync(id);

                if (exist != null)
                {
                    db.Decks.Remove(exist);
                    await db.SaveChangesAsync();
                }
            }
        }
Beispiel #5
0
        /// <summary>
        /// сохранить новый порядок карт
        /// </summary>
        /// <param name="deck"></param>
        /// <returns></returns>
        private async Task SaveMixingDeck(DeckDetailResponse deck)
        {
            using (var db = new DeckContext())
            {
                var exist = await db.Decks.FindAsync(deck.Id);

                if (exist != null)
                {
                    exist.CardsIds   = deck.CardsIds;
                    exist.DateModify = DateTime.UtcNow;
                    await db.SaveChangesAsync();
                }
            }
        }
Beispiel #6
0
        /// <summary>
        /// изменить значение
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public async Task <Value> EditValue(Value value)
        {
            using (var db = new DeckContext())
            {
                var exist = await db.Values.FindAsync(value.Id);

                if (exist != null && exist.Title != value.Title)
                {
                    exist.Title = value.Title;
                    await db.SaveChangesAsync();
                }

                return(exist);
            }
        }
Beispiel #7
0
 /// <summary>
 /// создать значение
 /// </summary>
 /// <param name="request"></param>
 /// <returns></returns>
 public async Task CreateValue(CreateValueRequest request)
 {
     using (var db = new DeckContext())
     {
         var exist = db.Values.FirstOrDefault(x => x.Title.ToLower() == request.Title.ToLower());
         if (exist != null)
         {
             throw new Exception($"value with title {request.Title} exist");
         }
         db.Values.Add(new Value {
             Title = request.Title
         });
         await db.SaveChangesAsync();
     }
 }
Beispiel #8
0
        /// <summary>
        /// изменить масть
        /// </summary>
        /// <param name="suit"></param>
        /// <returns></returns>
        public async Task <Suit> EditSuit(Suit suit)
        {
            using (var db = new DeckContext())
            {
                var exist = await db.Suits.FindAsync(suit.Id);

                if (exist != null && exist.Title != suit.Title)
                {
                    exist.Title = suit.Title;
                    await db.SaveChangesAsync();
                }

                return(exist);
            }
        }
Beispiel #9
0
 /// <summary>
 /// создание карты
 /// </summary>
 /// <param name="request"></param>
 /// <returns></returns>
 public async Task CreateCard(CreateCardRequest request)
 {
     using (var db = new DeckContext())
     {
         if (long.TryParse(request.SelectedSuitId.ToString(), out long suitId) &&
             suitId > 0 &&
             long.TryParse(request.SelectedValueId.ToString(), out long valueId) &&
             valueId > 0)
         {
             db.Cards.Add(new Card {
                 SuitId = suitId, ValueId = valueId
             });
             await db.SaveChangesAsync();
         }
     }
 }
Beispiel #10
0
        /// <summary>
        /// удалить значение
        /// </summary>
        /// <param name="id">ид значения</param>
        /// <returns></returns>
        public async Task DeleteValue(long id)
        {
            using (var db = new DeckContext())
            {
                var exist = await db.Values.FindAsync(id);

                if (exist != null)
                {
                    var cards = db.Cards.ToList();
                    foreach (var card in cards)
                    {
                        if (card.ValueId == id)
                        {
                            var cardService = new CardService();
                            await cardService.DeleteCard(card.Id);
                        }
                    }

                    db.Values.Remove(exist);
                    await db.SaveChangesAsync();
                }
            }
        }