public async Task CreateCondition_CreatesNewCondition()
        {
            //Arrange
            Guid      id           = new Guid("a818b39c-94a0-451a-bd4d-74c65204f79e");
            Condition newCondition = new Condition
            {
                Id           = id,
                Grade        = "MS64",
                LanguageCode = "en-US"
            };

            //Act
            await _conditionService.CreateCondition(newCondition);

            await _unitOfWork.Save();

            //Assert
            Assert.Equal(5, (await _conditionService.FindConditions(resourceParameters)).Count());
            Assert.Equal("MS64", (await _conditionService.FindConditionById(id)).Grade);
        }
Exemple #2
0
        public async Task <IActionResult> CreateCondition([FromBody] ConditionCreationDto condition,
                                                          [FromHeader(Name = "Accept")] string mediaType)
        {
            if (condition == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            var newCondition = _mapper.Map <Condition>(condition);
            await _conditionService.CreateCondition(newCondition);

            if (!await _conditionService.Save())
            {
                throw new Exception("Creating a condition failed on save.");
            }

            var returnedCondition = _mapper.Map <ConditionDto>(newCondition);

            if (mediaType == "application/json+hateoas")
            {
                var links          = CreateConditionLinks(returnedCondition.Id, null);
                var linkedResource = returnedCondition.ShapeData(null) as IDictionary <string, object>;

                linkedResource.Add("links", links);

                return(CreatedAtRoute("GetCondition",
                                      new { id = returnedCondition.Id },
                                      linkedResource));
            }
            else
            {
                return(CreatedAtRoute("GetCondition",
                                      new { id = returnedCondition.Id },
                                      returnedCondition));
            }
        }