Ejemplo n.º 1
0
        public void CreateCardAndDeleteCard()
        {
            //-- Arrange
            var newCard = new CardForCreationDto
            {
                Title       = "new card",
                Description = "description"
            };
            var deckId = 5;

            //-- Act
            var response = _cards.CreateCardInDeck(deckId, newCard, jsonMediaType).Result;

            var createdAtRouteResult = response as CreatedAtRouteResult;

            var newCardId   = (int)createdAtRouteResult.RouteValues["cardId"];
            var createdCard = _flashCardRepository.GetCard(newCardId);

            //-- Assert
            Assert.IsNotNull(createdAtRouteResult);
            Assert.AreEqual(deckId, createdCard.DeckId);
            Assert.AreEqual("new card", createdCard.Title);
            Assert.AreEqual("description", createdCard.Description);


            /*--------- delete the card ------------*/
            //-- Act
            _cards.DeleteCardInDeck(deckId, newCardId);

            var getResponse = _cards.GetCardInDeck(deckId, newCardId, jsonMediaType).Result;

            //-- Assert
            Assert.IsNotNull(getResponse as NotFoundResult);
        }
Ejemplo n.º 2
0
        public void CreateCardNoneExistingDeck()
        {
            //-- Arrange
            var newCard = new CardForCreationDto
            {
                Title       = "new card",
                Description = "description"
            };
            var noneExistingDeckId = 1;

            //-- Act
            var response = _cards.CreateCardInDeck(noneExistingDeckId, newCard, jsonMediaType).Result;

            //-- Assert
            Assert.IsNotNull(response as NotFoundResult);
        }
Ejemplo n.º 3
0
        public ActionResult <CardDto> CreateCardInDeck(int deckId, CardForCreationDto card,
                                                       [FromHeader(Name = "Accept")] string mediaType)
        {
            if (!MediaTypeHeaderValue.TryParse(mediaType,
                                               out MediaTypeHeaderValue parsedMediaType))
            {
                return(BadRequest());
            }

            var includeLinks = parsedMediaType.SubTypeWithoutSuffix
                               .EndsWith("hateoas", StringComparison.InvariantCultureIgnoreCase);

            if (!_flashCardRepository.DeckExists(deckId))
            {
                return(NotFound());
            }

            var cardEntity = _mapper.Map <Entities.Card>(card);

            _flashCardRepository.AddCard(cardEntity, deckId);
            _flashCardRepository.Save();

            var cardToReturn = _mapper.Map <CardDto>(cardEntity);

            if (!includeLinks)
            {
                return(CreatedAtRoute(nameof(GetCardInDeck),
                                      new { deckId, cardId = cardToReturn.Id },
                                      cardToReturn));
            }

            var links         = CreateLinksForCard(deckId, cardToReturn.Id);
            var cardWithLinks = new CardDtoWithLinks(cardToReturn, links);

            return(CreatedAtRoute(nameof(GetCardInDeck),
                                  new { deckId, cardId = cardToReturn.Id },
                                  cardWithLinks));
        }
Ejemplo n.º 4
0
        public IActionResult CreateCard(int taskboardId, [FromBody] CardForCreationDto card)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!_ninjaOrganizerRepository.TaskboardExists(taskboardId))
            {
                return(NotFound());
            }

            var finalCard = _mapper.Map <Entities.Card>(card);

            _ninjaOrganizerRepository.AddCardForTaskboard(taskboardId, finalCard);
            _ninjaOrganizerRepository.Save();

            var createdCardToReturn = _mapper.Map <Models.CardDto>(finalCard);

            // get this card
            return(CreatedAtRoute("GetCard",
                                  new { taskboardId, id = createdCardToReturn.Id },
                                  createdCardToReturn));
        }