public async Task <ActionResult <Product> > Edit(int id, [FromBody] ProductModel model) { Product product = _context.Products.Include("Photos").FirstOrDefault(x => x.ID == id); using (CTX db = _context) { if (product != null) { List <Photo> added_photos = new List <Photo>(); try { foreach (var x in product.Photos) { InitStaticFiles.DeleteImageByFileName(_env, _configuration, new string[] { "ImagesPath", "ImagesPathProduct" }, x.Path); } foreach (var photo in model.ImgsBase64) { string imageName = Path.GetRandomFileName() + ".jpg"; string pathSaveImages = InitStaticFiles .CreateImageByFileName(_env, _configuration, new string[] { "ImagesPath", "ImagesPathProduct" }, imageName, photo); added_photos.Add(new Photo { Path = imageName }); } } catch (Exception) { return(BadRequest()); } product.Name = model.Name; product.Description = model.Description; product.Price = model.Price; product.Photos.Clear(); product.Photos = added_photos; db.SaveChanges(); return(Ok(product)); } else { return(BadRequest()); } } }
public ActionResult DeleteProduct(int id) { Product dproduct = _context.Products.Include("Photos").FirstOrDefault(x => x.ID == id); if (dproduct != null) { _context.Products.Remove(dproduct); foreach (var x in dproduct.Photos) { InitStaticFiles.DeleteImageByFileName(_env, _configuration, new string[] { "ImagesPath", "ImagesPathProduct" }, x.Path); } _context.SaveChanges(); return(Ok()); } else { return(BadRequest()); } }
public async Task <IActionResult> RegisterUser([FromBody] RegisterDTO model) { // Auto return errors from viewModel and other global errors return(await HandleRequestAsync(async() => { if (!CaptchaHelper.VerifyAndExpireSolution(this.HttpContext, model.CaptchaKey, model.CaptchaText)) { var invalid = new Dictionary <string, string>(); invalid.Add("captchaText", "Помилка вводу зображення на фото"); return BadRequest(invalid); } var user = _userManager.FindByEmailAsync(model.Email).Result; if (user != null) { var invalid = new Dictionary <string, string>(); invalid.Add("email", "Користувач з даною електронною поштою уже зареєстрований"); return BadRequest(invalid); } string imageName = Path.GetRandomFileName() + ".jpg"; try { this._logger.LogDebug("Start register method RegisterUser..."); string pathSaveImages = InitStaticFiles .CreateImageByFileName(_env, _configuration, new string[] { "ImagesPath", "ImagesPathUser" }, imageName, model.Photo); this._logger.LogDebug("Save image complete method RegisterUser..."); if (pathSaveImages != null) { var profile = new UserProfile() { RegistrationDate = DateTime.Now, FirstName = model.FirstName, LastName = model.LastName, Photo = imageName, }; user = new DbUser() { UserName = model.Email, Email = model.Email, PhoneNumber = model.Phone, UserProfile = profile }; var result = _userManager.CreateAsync(user, model.Password).Result; if (!result.Succeeded) { var errors = CustomValidator.GetErrorsByIdentityResult(result); return BadRequest(errors); } await _signInManager.SignInAsync(user, isPersistent: false); // Return token JwtInfo jwtInfo = new JwtInfo() { Token = _jwtTokenService.CreateToken(user), RefreshToken = _jwtTokenService.CreateRefreshToken(user) }; this._logger.LogDebug("End method RegisterUser..."); return Ok(jwtInfo); } else { throw new Exception("Помила додавання фото в БД"); } } catch (Exception ex) { InitStaticFiles.DeleteImageByFileName(_env, _configuration, new string[] { "ImagesPath", "ImagesPathUser" }, imageName); var errors = new Dictionary <string, string>(); errors.Add("invalid", ex.Message); return BadRequest(errors); } })); }