public IActionResult About()//test function { var addr = new Address { City = "London", Country = "UK", Street = "ABC", Number = 818 }; var owner = new Owner { FullName = "Sayed Ali", Job = "Accountant", Avatar = "sayed.jpg", Address = addr, }; var item = new PortofolioItem { Name = "Calculations", Description = "Full report of company", ImageUrl = "portofolio5.jpg", Owner = owner }; _uow.AddressRepository.Insert(addr); _uow.OwnerRepository.Insert(owner); _uow.PortofolioItemRepository.Insert(item); _uow.Save(); return(View()); }
public void AddPortofolioItem(PortofolioItem item, string userId) { Owner owner = GetPortofolioByUser(new Guid(userId)); item.Owner = owner; PortofolioItemRepository.Insert(item); Save(); }
public ActionResult EditPortofolioItem(PortofolioItemViewModel model) { try { var userId = this.User.FindFirstValue(ClaimTypes.NameIdentifier); string FileName = model.ImageFile; if (model.ImageUploader != null) { FileName = model.ImageUploader.FileName.Split(@"\", StringSplitOptions.None).Last(); string uploadFolder = Path.Combine(hosting.WebRootPath, @"img\portfolio"); string FullPath = Path.Combine(uploadFolder, FileName); model.ImageUploader.CopyTo(new FileStream(FullPath, FileMode.Create)); } else { if (model.ImageFile == "") { ModelState.AddModelError("upload", "Please upload an image"); } } if (!ModelState.IsValid) { ModelState.AddModelError("", "please fix all errors"); return(View(model));//return data as it is } /// var item = new PortofolioItem { Description = model.Description, Name = model.Name, ImageUrl = FileName, Id = model.ItemId, OwnerId = model.OwnerId }; uow.UpdatePortofolioItem(item); return(RedirectToAction("ShowPortofolioItems")); } catch (Exception ex) { return(View(model)); } }
public IActionResult Edit(Guid id, PortfolioViewModel model) { if (id != model.Id) { return(NotFound()); } if (ModelState.IsValid) { try { if (model.File != null) { string uploads = Path.Combine(_hosting.WebRootPath, @"img\portfolio"); string fullPath = Path.Combine(uploads, model.File.FileName); model.File.CopyTo(new FileStream(fullPath, FileMode.Create)); } PortofolioItem portofolioItem = new PortofolioItem { Id = model.Id, ProjectName = model.ProjectName, Description = model.Description, ImageUrl = model.File.FileName }; _portfolio.Entity.Update(portofolioItem); _portfolio.Save(); } catch (DbUpdateConcurrencyException) { if (!PortofolioItemExists(model.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(model)); }
public async Task <IActionResult> Create(PortfolioViewModel model) { if (ModelState.IsValid) { if (model.File != null) { string uploads = Path.Combine(_hosting.WebRootPath, @"img\portfolio"); string fullPath = Path.Combine(uploads, model.File.FileName); model.File.CopyTo(new FileStream(fullPath, FileMode.Create)); } PortofolioItem portofolioItem = new PortofolioItem { ProjectName = model.ProjectName, Description = model.Description, ImageUrl = model.File.FileName }; _portfolio.Entity.Insert(portofolioItem); _portfolio.Save(); return(RedirectToAction(nameof(Index))); } return(View(model)); }
protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); builder.Entity <Owner>().Property(x => x.Id).HasDefaultValueSql("NEWID()"); builder.Entity <PortofolioItem>().Property(x => x.Id).HasDefaultValueSql("NEWID()"); builder.Entity <Address>().Property(x => x.Id).HasDefaultValueSql("NEWID()"); Address address = new Address { Id = Guid.NewGuid(), City = "Cairo", Country = "Egypt", Number = 1, Street = "Ibrahim Bik Al Kabeer" }; builder.Entity <Address>().HasData(address); /// Owner Me = new Owner { Id = Guid.NewGuid(), FullName = "go to Home/Index to login", Avatar = "dafault.jpg", Job = "Dashboard/Create(Edit)Profile - Dashboard/ShowPortofolioItems - Portofolio/Index/Guid? ", AddressId = address.Id }; builder.Entity <Owner>().HasData(Me); /// PortofolioItem item = new PortofolioItem { Id = Guid.NewGuid(), Name = "portofolio1", Description = "description of portrofolio1", ImageUrl = "portofolio1.jpg", OwnerId = Me.Id }; builder.Entity <PortofolioItem>().HasData(item); //here we cant do this.SaveChanges () and cant add element to any DbSet, so we generate the Key Values in c# and put then in the FK values as you see }
public void UpdatePortofolioItem(PortofolioItem item) { PortofolioItemRepository.Update(item); Save(); }