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> 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 void AddLaptop(LaptopModel laptopModel) { Laptop laptop = new Laptop { BrandId = laptopModel.BrandId, ImageUrl = laptopModel.ImageUrl, GraphicCard = laptopModel.GraphicCard, LaptopName = laptopModel.LaptopName, Price = laptopModel.Price, Quantity = laptopModel.Quantity, Ram = laptopModel.Ram, SeriesCPU = laptopModel.SeriesCPU, Storage = laptopModel.Storage, Weight = laptopModel.Weight, }; laptopRepository.Add(laptop); }