public async Task <IActionResult> Add([FromBody] ADFullDataModel model) { #region Check user var userID = HttpContext.User.Identity.Name; if (userID == null) { return(StatusCode(StatusCodes.Status401Unauthorized)); } ApplicationUser user = await _context.Set <ApplicationUser>().SingleOrDefaultAsync(item => item.UserName == userID); Account account = _context.Set <Account>() .Include(x => x.ADs) .FirstOrDefault(x => x.ID == user.AccountID); if (user == null || account == null) { return(null); } #endregion if (model == null || ModelState.IsValid == false) { return(BadRequest("Unvalid data model")); } try { #region Create new AD AD newAd = new AD() { Name = model.Name, Title = model.Title, Description = model.Description, CategoryID = model.CategoryID, CreationDate = DateTime.Now, Radius = 10, Isapprove = false, ADViews = 0, NumberViews = 0, IsNegotiable = model.IsNegotiable, Email = model.Email, Phone = model.Phone, Longitude = model.Longitude, Latitude = model.Latitude, AccountID = account.ID, IsDisabled = false, Price = (model.Price ?? 0) < 0 ? 0 : model.Price, CurrencyID = _context.Set <Currency>().FirstOrDefault(x => x.Code == model.CurrencyName)?.ID, PriceType = PriceType.Cash.ToString(), ContactWay = ContactWay.Both.ToString(), PublishedDate = DateTime.Now }; bool codeGenerated = await newAd.GenerateCode(_context); if (codeGenerated == false) { return(BadRequest("Error in generate code")); } #endregion #region Ad category fields if (model.CategoryFieldOptions != null && model.CategoryFieldOptions.Count > 0) { foreach (var item in model.CategoryFieldOptions) { var CatField = _context.Set <CategoryField>().Include(x => x.CategoryFieldOptions).SingleOrDefault(S => S.ID == item.CategoryFieldID); CategoryFieldOption catOption = CatField.CategoryFieldOptions.FirstOrDefault(x => x.ID == item.CategoryFieldOptionID); if (CatField == null) { continue; } newAd.ADExtraFields.Add(new AdExtraField { CategoryFieldOptionID = catOption?.ID, Value = catOption?.Name ?? item.Value, Name = CatField.Name }); } } #endregion #region Save Main Image if (model.MainImage != null && model.MainImage.Length > 0) { string filename = ImageOperations.SaveImage(model.MainImage, newAd); if (!string.IsNullOrWhiteSpace(filename)) { newAd.ADImages.Add(new ADImage() { ImagePath = filename, IsMain = true }); } } #endregion #region Save Other images if (model.Images != null && model.Images.Any(x => x.Length > 0)) { foreach (var file in model.Images.Where(x => x.Length > 0)) { string filename = ImageOperations.SaveImage(file, newAd); if (!string.IsNullOrWhiteSpace(filename)) { newAd.ADImages.Add(new ADImage() { ImagePath = filename, IsMain = false }); } } } #endregion _context.Set <AD>().Add(newAd); await _context.SaveChangesAsync(); return(Ok(newAd.Code)); } catch (Exception e) { return(BadRequest(e.Message)); } }
public async Task <IActionResult> Update([FromBody] ADFullDataModel model, Guid ID) { #region Check user var userID = HttpContext.User.Identity.Name; if (userID == null) { return(StatusCode(StatusCodes.Status401Unauthorized)); } ApplicationUser user = await _context.Set <ApplicationUser>().SingleOrDefaultAsync(item => item.UserName == userID); Account account = _context.Set <Account>() .Include(x => x.ADs) .FirstOrDefault(x => x.ID == user.AccountID); if (user == null || account == null) { return(null); } #endregion if (model == null || ModelState.IsValid == false) { return(BadRequest("Unvalid data model")); } var UpAd = _context.Set <AD>() .Include(x => x.Category) .Include(x => x.Account) .Include(x => x.ADExtraFields) .Include(x => x.ADImages) .SingleOrDefault(s => s.ID == ID); if (UpAd == null) { return(BadRequest("No Ad related to this ID")); } try { UpAd.CategoryID = model.CategoryID; UpAd.Name = model.Name; UpAd.Title = model.Title; UpAd.Description = model.Description; UpAd.Email = model.Email; UpAd.Phone = model.Phone; UpAd.IsDisabled = false; UpAd.Price = model.Price ?? 0; UpAd.Longitude = model.Longitude; UpAd.Latitude = model.Latitude; UpAd.IsNegotiable = model.IsNegotiable; UpAd.CurrencyID = _context.Set <Currency>().FirstOrDefault(x => x.Code == model.CurrencyName)?.ID; _context.Set <AdExtraField>().RemoveRange(UpAd.ADExtraFields); _context.Set <ADImage>().RemoveRange(UpAd.ADImages); #region Update Category fields if (model.CategoryFieldOptions != null && model.CategoryFieldOptions.Count > 0) { foreach (var item in model.CategoryFieldOptions) { var CatField = _context.Set <CategoryField>().Include(x => x.CategoryFieldOptions).SingleOrDefault(S => S.ID == item.CategoryFieldID); CategoryFieldOption catOption = CatField.CategoryFieldOptions.FirstOrDefault(x => x.ID == item.CategoryFieldOptionID); if (CatField == null) { continue; } UpAd.ADExtraFields.Add(new AdExtraField { CategoryFieldOptionID = catOption?.ID, Value = catOption?.Name ?? item.Value, Name = CatField.Name }); } } #endregion #region Update main images if (model.MainImage != null && model.MainImage.Length > 0) { string filename = ImageOperations.SaveImage(model.MainImage, UpAd); if (!string.IsNullOrWhiteSpace(filename)) { UpAd.ADImages.Add(new ADImage() { ImagePath = filename, IsMain = true }); } } #endregion #region Save Other images if (model.Images != null && model.Images.Any(x => x.Length > 0)) { foreach (var file in model.Images.Where(x => x.Length > 0)) { string filename = ImageOperations.SaveImage(file, UpAd); if (!string.IsNullOrWhiteSpace(filename)) { UpAd.ADImages.Add(new ADImage() { ImagePath = filename, IsMain = false }); } } } #endregion await _context.SubmitAsync(); return(Ok()); } catch (Exception e) { return(BadRequest(e.Message)); } }