public async Task <IActionResult> PlaceOrder([FromBody] OrderForCreateDto orderForCreateDto) { var order = _mapper.Map <Order>(orderForCreateDto); if (Request.Headers.ContainsKey("id")) { var user = await _repo.GetUserById(int.Parse(Request.Headers["id"].First())); user.Orders.Add(order); } foreach (var item in orderForCreateDto.ListOfOrderDetailDto) { var orderDetail = new OrderDetail(); var product = await _repo.GetProductById(item.ProductId); orderDetail.PricePerUnit = item.PricePerUnit; orderDetail.Quantity = item.Quantity; orderDetail.Product = product; orderDetail.ProductShortName = item.ProductShortName; order.OrderDetails.Add(orderDetail); } _repo.Add(order); if (await _repo.SaveAll()) { return(CreatedAtRoute(nameof(GetOrderById), new { controller = "Order", id = order.Id }, order)); } throw new Exception($"Error, cannot create your order"); }
public async Task <IActionResult> UpdatePersonalInfo([FromBody] PersonalInfoForUpdateDto personalInfoForUpdateDto) { if (!Request.Headers.ContainsKey("id")) { return(Unauthorized()); } int id = int.Parse(Request.Headers["id"].First()); if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier)?.Value)) { return(Unauthorized()); } var user = await _repo.GetUserById(id); if (user == null) { return(NotFound()); } var updatedUser = _mapper.Map(personalInfoForUpdateDto, user); if (await _repo.SaveAll()) { return(NoContent()); } throw new Exception($"Updating user {id} failed on save"); }
public async Task <IActionResult> AddBannerImage([FromForm] ImageForCreateDto imageForCreateDto) { var file = imageForCreateDto.File; var uploadResult = new ImageUploadResult(); if (file.Length > 0) { using (var stream = file.OpenReadStream()) { var uploadParams = new ImageUploadParams() { File = new FileDescription(file.Name, stream), Transformation = new Transformation().Width(1920).Height(600).Crop("fill") }; uploadResult = _cloudinary.Upload(uploadParams); } } imageForCreateDto.Url = uploadResult.Url.ToString(); imageForCreateDto.PublicId = uploadResult.PublicId; var image = _mapper.Map <Image>(imageForCreateDto); image.IsThumbnail = false; image.IsBanner = true; _repo.Add(image); if (await _repo.SaveAll()) { return(StatusCode(201)); } return(BadRequest("Could not add the image")); }
public async Task <IActionResult> AddBrand([FromBody] BrandForCreateDto brandForCreateDto) { var brand = new Brand(brandForCreateDto.BrandName); _repo.Add(brand); if (await _repo.SaveAll()) { return(CreatedAtRoute(nameof(GetBrandById), new { brand.Id }, brand)); } return(BadRequest("Could not add new brand")); }
public async Task <IActionResult> AddProduct([FromBody] ProductForCreateDto productForCreateDto) { var product = _mapper.Map <Product>(productForCreateDto); var brand = await _repo.GetBrandById(productForCreateDto.BrandId); brand.Products.Add(product); if (await _repo.SaveAll()) { return(CreatedAtRoute("GetProductById", new { Controller = "Product", id = product.Id }, product)); } return(BadRequest("Could not add new product")); }
public async Task <IActionResult> AddImageForProduct(int productId, [FromForm] ImageForCreateDto imageForCreateDto) { var product = await _repo.GetProductById(productId); if (product == null) { return(NotFound()); } var file = imageForCreateDto.File; var uploadResult = new ImageUploadResult(); if (file.Length > 0) { using (var stream = file.OpenReadStream()) { var uploadParams = new ImageUploadParams() { File = new FileDescription(file.Name, stream) // Transformation = new Transformation().Width(200).Height(200).Crop("fill") }; uploadResult = _cloudinary.Upload(uploadParams); } } imageForCreateDto.Url = uploadResult.Url.ToString(); imageForCreateDto.PublicId = uploadResult.PublicId; var image = _mapper.Map <Image>(imageForCreateDto); image.IsThumbnail = product.Images.Any(i => i.IsThumbnail) ? false : true; product.Images.Add(image); if (await _repo.SaveAll()) { var routeValues = new { productId, image.Id }; return(CreatedAtRoute(nameof(GetPrductImageById), routeValues, image)); } return(BadRequest("Could not add the image")); }
public async Task <IActionResult> AddImageForBrand(int brandId, [FromForm] ImageForCreateDto imageForCreateDto) { var brand = await _repo.GetBrandById(brandId); if (brand == null) { return(NotFound()); } var file = imageForCreateDto.File; var uploadResult = new ImageUploadResult(); if (file.Length > 0) { using (var stream = file.OpenReadStream()) { var uploadParams = new ImageUploadParams() { File = new FileDescription(file.Name, stream) }; uploadResult = _cloudinary.Upload(uploadParams); } } imageForCreateDto.Url = uploadResult.Url.ToString(); imageForCreateDto.PublicId = uploadResult.PublicId; var image = _mapper.Map <Image>(imageForCreateDto); image.IsThumbnail = true; brand.Image = image; if (await _repo.SaveAll()) { var routeValues = new { brandId, image.Id }; return(CreatedAtRoute(nameof(GetBrandImageById), routeValues, image)); } return(BadRequest("Could not add the image")); }