public int Create(Hero hero) { hero.Photo = "photos/unknown.jpg"; var addedHero = this.context.Heroes.Add(hero); this.context.SaveChanges(); return addedHero.Entity.Id; }
public int Update(int id, Hero hero) { var existedHero = this.context.Heroes.SingleOrDefault(h => h.Id == id); existedHero.FirstName = String.IsNullOrEmpty(hero.FirstName) ? existedHero.FirstName : hero.FirstName; existedHero.LastName = String.IsNullOrEmpty(hero.LastName) ? existedHero.LastName : hero.LastName; existedHero.Pseudonym = String.IsNullOrEmpty(hero.Pseudonym) ? existedHero.Pseudonym : hero.Pseudonym; existedHero.Photo = String.IsNullOrEmpty(hero.Photo) ? existedHero.Photo : hero.Photo; this.context.SaveChanges(); return existedHero.Id; }
public void Post(int id, IFormFile file) { if (file.Length > 0) { var uploads = "photos"; var uploadsAbsPath = Path.Combine(env.WebRootPath, uploads); var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'); var relFileName = Path.Combine(uploads, fileName); var hero = new Hero { Photo = relFileName }; this.service.Update(id, hero); file.SaveAs(Path.Combine(uploadsAbsPath, fileName)); } }