Example #1
0
        public async Task <IActionResult> Create([FromBody] CreateReviewRequestDto dto)
        {
            var reviewToCreate = _mapper.Map <Review>(dto);

            var criteria = await _criteriasRepository.GetCriteriaByName(dto.CriteriaName);

            if (criteria == null)
            {
                criteria = new Criteria {
                    Name = dto.CriteriaName
                };
                _criteriasRepository.Create(criteria);
            }
            reviewToCreate.Criteria = criteria;

            _reviewsRepository.Create(reviewToCreate);

            if (await _reviewsRepository.SaveAllChangesAsync())
            {
                var reviewFromDb = await _reviewsRepository.GetByIdAsync(reviewToCreate.Id);

                var droToReturn = _mapper.Map <ReviewDto>(reviewFromDb);
                return(CreatedAtRoute(nameof(GetReview), new { id = reviewToCreate.Id }, droToReturn));
            }

            return(BadRequest("Could not create a review."));
        }
        public async Task <IActionResult> Create([FromBody] CreateCriteriaRequestDto dto)
        {
            var existCriteria = await _repository.GetCriteriaByName(dto.Name);

            if (existCriteria != null)
            {
                return(BadRequest($"Criteria with name {dto.Name} already exists."));
            }

            var criteriaToCreate = _mapper.Map <Criteria>(dto);

            _repository.Create(criteriaToCreate);

            if (await _repository.SaveAllChangesAsync())
            {
                var criteriaToReturn = _mapper.Map <CriteriaDto>(criteriaToCreate);
                return(CreatedAtRoute(nameof(GetCriteria), new { id = criteriaToCreate.Id }, criteriaToReturn));
            }

            return(BadRequest("Could not create a criteria."));
        }