示例#1
0
        public async Task <CardDto> AddCard(Guid userId, long boxId, long trayId, CardCreationData data)
        {
            using (Context)
            {
                ////var user = await GetUserById(userId);
                var box = await Context.Boxes
                          .Include(b => b.User)
                          .FirstOrDefaultAsync(b => b.Id == boxId && b.User.Guid == userId);

                var tray = await Context.Trays
                           .Where(t => t.Box == box && t.Id == trayId)
                           .FirstOrDefaultAsync();

                var card = new Card
                {
                    Answer   = data.Answer,
                    Question = data.Question,
                    Tray     = tray,
                    User     = box.User
                };

                var newCard = await Context.Cards.AddAsync(card);

                await Context.SaveChangesAsync();

                return(newCard.Entity.ToCardDto());
            }
        }
示例#2
0
        public async Task <IActionResult> AddCard(
            [FromRoute] long boxId,
            [FromRoute] long trayId,
            [FromBody] CardCreationData data)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                var card = await _boxService.AddCard(User.GetId(), boxId, trayId, data);

                return(CreatedAtAction(nameof(GetCard), new { boxId, trayId, cardId = card.Id }, card));
            }
            catch (Exception e)
            {
                return(BadRequest(e));
            }
        }