Exemple #1
0
        public async Task <ActionResult <ProductTypeDto> > CreateProductType(CreateUpdateTypeDto createDto)
        {
            if (_repository.IsExist(x => x.Name.ToLower() == createDto.Name.ToLower()))
            {
                return(BadRequest(new ApiResponse(400, $"{createDto.Name} already exists")));
            }

            var type = _mapper.Map <ProductType>(createDto);

            type.Id        = Guid.NewGuid().ToString();
            type.CreatedBy = HttpContext.User.RetrieveEmailFromPrincipal();
            await _repository.Add(type);

            if (await _unitOfWork.Complete() <= 0)
            {
                return(BadRequest(new ApiResponse(400, $"An error occured while trying to insert")));
            }

            return(CreatedAtRoute("GetProductType", new { productId = type.Id },
                                  _mapper.Map <ProductTypeDto>(type)));
        }
Exemple #2
0
        public async Task <ActionResult <ProductTypeDto> > UpdateProductType(string typeId, CreateUpdateTypeDto updateDto)
        {
            var type = await _repository.GetByIdAsync(typeId);

            if (type == null)
            {
                return(NotFound(new ApiResponse(404)));
            }

            _mapper.Map(updateDto, type);
            _repository.Update(type);

            if (await _unitOfWork.Complete() <= 0)
            {
                return(BadRequest(new ApiResponse(400, $"An error occured while trying to update")));
            }

            return(CreatedAtRoute("GetProductType", new { bandId = type.Id },
                                  _mapper.Map <ProductTypeDto>(type)));
        }