public async Task <IActionResult> AddManufacturer(Manufacturer manufacturer) { _repo.Add <Manufacturer>(manufacturer); await _repo.SaveAll(); return(CreatedAtAction("GetManufacturers", new { id = manufacturer.IDManufacturer }, manufacturer)); }
public async Task <IActionResult> UpdateProduct(int id, ProductForUpdateDto productForUpdateDto) { var productFromRepo = await _repo.GetProduct(id); _mapper.Map(productForUpdateDto, productFromRepo); if (await _repo.SaveAll()) { return(NoContent()); } throw new Exception($"Cập nhật sản phẩm id= {id} thất bại."); }
public async Task <IActionResult> UpdateUser(int id, UserForUpdateDto userForUpdateDto) { if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var userFromRepo = await _repo.GetUser(id); _mapper.Map(userForUpdateDto, userFromRepo); if (await _repo.SaveAll()) { return(NoContent()); } throw new Exception($"Cập nhập user {id} thất bại"); }
public IActionResult Contact(ContactViewModel model) { try { if (ModelState.IsValid) { var contact = Mapper.Map <ContactViewModel, Contact>(model); rep.SaveEntity(contact); rep.SaveAll(); ViewBag.UserMessage = "Thanks for contacting us ,we will get back to you shortly"; } return(View()); } catch (Exception ex) { this.Logger.LogError(ex.Message); return(null); } }
public async Task <IActionResult> AddPhotoForProduct(int idProduct, [FromForm] PhotoForCreationDto photoForCreationDto) { var product = await _repo.GetProduct(idProduct); var file = photoForCreationDto.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(500).Height(500).Crop("fill").Gravity("face") }; uploadResult = _cloudinary.Upload(uploadParams); } } photoForCreationDto.Url = uploadResult.Uri.ToString(); photoForCreationDto.PublicId = uploadResult.PublicId; var photo = _mapper.Map <Photo>(photoForCreationDto); if (!product.Photos.Any(u => u.IsMain)) { photo.IsMain = true; } product.Photos.Add(photo); if (await _repo.SaveAll()) { var photoToReturn = _mapper.Map <PhotoForReturnDto>(photo); return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, photoToReturn)); } return(BadRequest("Could not add the photo")); }
public async Task <IActionResult> UpdateOrder(int id, int state) { var orderFromRepo = await _repo.GetOrderById(id); var orderUpated = orderFromRepo; orderUpated.OrderState = state; _mapper.Map(orderUpated, orderFromRepo); if (await _repo.SaveAll()) { return(NoContent()); } throw new Exception($"Duyệt hóa đơn = {id} thất bại."); }