public IActionResult Add(BrandCreateDto brandCreateDto) { var brand = mapper.Map <Brand>(brandCreateDto); brandService.Add(brand); return(NoContent()); }
public ActionResult <BrandReadDto> CreateBrand(BrandCreateDto brandCreateDto, [FromHeader(Name = "Accept")] string mediaType) { if (!MediaTypeHeaderValue.TryParse(mediaType, out MediaTypeHeaderValue parsedMediaType)) { return(BadRequest()); } Brand createdBrand = _mapper.Map <Brand>(brandCreateDto); _repository.AddBrand(createdBrand); _repository.SaveChanges(); BrandReadDto brandReadDto = _mapper.Map <BrandReadDto>(createdBrand); if (parsedMediaType.MediaType == "application/hateoas+json") { var links = CreateLinksForBrand(brandReadDto.Id); var propsDictionary = brandReadDto.ShapeData(null) as System.Collections.Generic.IDictionary <string, object>; propsDictionary.Add("links", links); return(CreatedAtRoute("GetBrandById", new { brandId = propsDictionary["Id"] }, propsDictionary)); } return(Ok(brandReadDto)); }
public async Task <IActionResult> CreateBrand([FromBody] BrandCreateDto brand) { try { if (brand == null) { return(BadRequest("Company object is null")); } if (!ModelState.IsValid) { return(BadRequest("Invalid company object")); } // convert incoming Dto to actual Model instance var brandEntity = _mapper.Map <CompanyBrandModel>(brand); _repository.Brand.CreateBrand(brandEntity); await _repository.Save(); // convert the model back to a DTO for output var createdBrand = _mapper.Map <BrandDto>(brandEntity); if (createdBrand.CompanyName == null) { var company = await _repository.Company.GetCompanyById(new Guid(createdBrand.CompanyId)); createdBrand.CompanyName = company.CompanyName; } return(Ok(createdBrand)); } catch (Exception) { return(StatusCode(500, "Something went wrong")); } }