public void Create(CreateItemVM item) { var mappedItem = mapper.Map <Item>(item); mappedItem.CreatedAt = DateTime.Now; context.Items.Add(mappedItem); context.SaveChanges(); }
private CreateItemVM PopulateDropDownLists(CreateItemVM model) { model.Countries = GetCountries(null); model.Regions = GetRegions(null); model.Cities = new List <SelectListItem>();//GetCities(); model.Categories = GetCategories(null); model.Sizes = GetSizes(0); return(model); }
public ActionResult Edit(Guid id) { CreateItemVM model = new CreateItemVM(); if (id != null) { model = PopulateEditItemVM(id); } return(View(model)); }
public ActionResult CreateItem() { CreateItemVM model = new CreateItemVM(); model.UserId = Guid.Parse(User.Identity.GetUserId()); model.OwnerEmail = User.Identity.GetUserName(); model.Countries = GetCountries(userCountry); model.Regions = GetRegions(userRegion); model.Cities = new List <SelectListItem>(); model.Categories = GetCategories(null); model.Sizes = GetSizes(0); return(View(model)); }
public async Task <IActionResult> CreateItem(CreateItemVM createItemVM) { if (!ModelState.IsValid) { return(View(createItemVM)); } var dtoModel = mapper.Map <CreateItemModel>(createItemVM); var result = await itemsManager.CreateAsync(dtoModel); if (!result.Succeed) { result.Errors.ToList().ForEach(e => ModelState.AddModelError("", e)); return(View(createItemVM)); } return(RedirectToAction(nameof(Store.Item), nameof(Store), new { id = result.CreatedItemId })); }
public IActionResult CreateItem(int?collectionId) { var model = new CreateItemVM(); var ownerId = User.IsInRole("admin") ? sessionHelper.GetRemeberedUserId() : sessionHelper.GetCurrentUserId(); if (ownerId == 0) { return(RedirectToAction(nameof(Home.Index), nameof(Home))); } model.OwnerId = ownerId; if (collectionId.HasValue) { model.CollectionId = collectionId; } return(View(model)); }
private CreateItemVM PopulateEditItemVM(Guid itemId) { var item = _dataManager.ItemGetById(itemId).Result; CreateItemVM model = new CreateItemVM(); model.ItemId = item.Id; model.UserId = Guid.Parse(User.Identity.GetUserId()); model.OwnerEmail = User.Identity.GetUserName(); var countries = GetCountries(_dataManager.CountriesGetAll().Result.FirstOrDefault().CountryName); model.Countries = countries; model.CountryId = new Guid(countries.Where(x => x.Selected == true).FirstOrDefault().Value); var regions = GetRegions(_dataManager.RegionGetById(item.City.RegionId).Result.RegionName); model.Regions = regions; model.RegionId = new Guid(regions.Where(x => x.Selected == true).FirstOrDefault().Value); var cities = GetCities(item.City.RegionId, item.City.CityName); model.Cities = cities; model.CityId = new Guid(cities.Where(x => x.Selected == true).FirstOrDefault().Value); var categories = GetCategories(_dataManager.CategoryById(item.CategoryId).Result.CategoryName); model.Categories = categories; model.CategoryId = new Guid(categories.Where(x => x.Selected == true).FirstOrDefault().Value); var sizes = GetSizes((int)item.Size); model.Sizes = sizes; model.Size = sizes.Where(x => x.Selected).FirstOrDefault().Value; model.Description = item.Description; model.Title = item.ItemName; model.RewardValue = item.RewardValue.ToString(); model.ImageLocation = item.ImagesLocation; model.CreatedOn = item.CreatedDate; model.IsLost = item.IsLost; return(model); }
public IActionResult Create(CreateItemVM model) { itemService.Create(model); return(Ok()); }
public async Task <ActionResult> Edit(CreateItemVM model) { try { if (!ModelState.IsValid) { if (model.ItemId.HasValue) { model = PopulateEditItemVM(model.ItemId.Value); } return(View(Task.FromResult(model))); } string directory = null; string userDirectory = null; string fileLocation = null; StringBuilder trailingPath = null; string newDirectory = null; string oldDirectory = null; if (model.ItemId.HasValue) { Item item = _dataManager.ItemGetById(model.ItemId.Value).Result; item.CityId = model.CityId; item.ImagesLocation = model.ImageLocation ?? ""; item.IsLost = model.IsLost; item.ItemName = model.Title; item.Description = model.Description; item.RewardValue = (float)Convert.ToDouble(model.RewardValue); item.CategoryId = model.CategoryId; item.ModifiedDate = DateTime.UtcNow; Sizes sizeType = (Sizes)Enum.Parse(typeof(Sizes), model.Size); switch (sizeType) { case Sizes.Голям: { item.Size = (int)Sizes.Голям; }; break; case Sizes.Малък: { item.Size = (int)Sizes.Малък; }; break; case Sizes.Среден: { item.Size = (int)Sizes.Среден; }; break; default: item.Size = 0; break; } if (model.file != null) { if (item.ImagesLocation != null) { int index = model.ImageLocation.LastIndexOf(@"/"); string str = model.ImageLocation.Substring(index + 1); oldDirectory = Path.Combine(directory + userDirectory + @"\" + str); } directory = Server.MapPath(@"~/images/"); userDirectory = User.Identity.Name; trailingPath = new StringBuilder(Path.GetExtension(model.file.FileName)); trailingPath.Insert(0, User.Identity.GetUserId()); fileLocation = Path.Combine(directory, userDirectory, trailingPath.ToString()); if (!Directory.Exists(directory + userDirectory)) { Directory.CreateDirectory(directory + userDirectory); } model.file.SaveAs(fileLocation); newDirectory = @"/images/" + userDirectory + "/" + trailingPath; if (item.ImagesLocation == null) { item.ImagesLocation = newDirectory; } if (item.ImagesLocation != newDirectory) { System.IO.File.Delete(oldDirectory); item.ImagesLocation = newDirectory; } } await _dataManager.ItemEdit(item); } return(RedirectToAction("Index")); } catch (Exception ex) { string msg = ex.Message; return(View()); } }
public async Task <ActionResult> CreateItem(CreateItemVM model) { try { TryUpdateModel(model); if (!ModelState.IsValid) { model = PopulateDropDownLists(model); return(View(model)); } string directory = null; string userDirectory = null; string fileLocation = null; StringBuilder trailingPath = null; string newDirectory = null; Item item = new Item(); if (!model.ItemId.HasValue) { item.UserId = User.Identity.GetUserId(); item.CityId = model.CityId; item.ImagesLocation = model.ImageLocation ?? ""; item.IsDeleted = false; item.IsLost = model.IsLost; item.ItemName = model.Title; item.Description = model.Description; item.RewardValue = (float)Convert.ToDouble(model.RewardValue); item.CategoryId = model.CategoryId; item.CreatedDate = DateTime.UtcNow; Sizes sizeType = (Sizes)Enum.Parse(typeof(Sizes), model.Size); switch (sizeType) { case Sizes.Голям: { item.Size = (int)Sizes.Голям; }; break; case Sizes.Малък: { item.Size = (int)Sizes.Малък; }; break; case Sizes.Среден: { item.Size = (int)Sizes.Среден; }; break; default: item.Size = 0; break; } if (model.file != null) { directory = Server.MapPath(@"~/images/"); userDirectory = User.Identity.Name; trailingPath = new StringBuilder(Path.GetExtension(model.file.FileName)); trailingPath.Insert(0, Guid.NewGuid()); fileLocation = Path.Combine(directory, userDirectory, trailingPath.ToString()); if (!Directory.Exists(directory + userDirectory)) { Directory.CreateDirectory(directory + userDirectory); } model.file.SaveAs(fileLocation); newDirectory = @"/images/" + userDirectory + "/" + trailingPath; item.ImagesLocation = newDirectory; await _dataManager.ItemAdd(item); } } } catch (Exception ex) { string msg = ex.Message; //log ex //throw err msg } return(RedirectToAction("Index")); }
public ActionResult MyItems() { CreateItemVM model = new CreateItemVM(); return(View(model)); }