public Yoyos.Dto.Yoyo CreateYoyo(Yoyo yoyo) { return new Yoyos.Dto.Yoyo() { Id = yoyo.Id, Name = yoyo.Name, Description = yoyo.Description, Created = yoyo.Created }; }
public RepositoryActionResult<Yoyo> UpdateYoyo(Yoyo y) { try { // you can only update when an expense already exists for this id var existingYoyo = _context.Yoyos.FirstOrDefault(yoyo => yoyo.Id == y.Id); if (existingYoyo == null) { return new RepositoryActionResult<Yoyo>(y, RepositoryActionStatus.NotFound); } // change the original entity status to detached; otherwise, we get an error on attach // as the entity is already in the dbSet // set original entity state to detached _context.Entry(existingYoyo).State = EntityState.Detached; // attach & save _context.Yoyos.Attach(y); // set the updated entity state to modified, so it gets updated. _context.Entry(y).State = EntityState.Modified; var result = _context.SaveChanges(); if (result > 0) { return new RepositoryActionResult<Yoyo>(y, RepositoryActionStatus.Updated); } else { return new RepositoryActionResult<Yoyo>(y, RepositoryActionStatus.NothingModified, null); } } catch (Exception ex) { return new RepositoryActionResult<Yoyo>(null, RepositoryActionStatus.Error, ex); } }
public RepositoryActionResult<Yoyo> InsertYoyo(Yoyo y) { try { _context.Yoyos.Add(y); var result = _context.SaveChanges(); if (result > 0) { return new RepositoryActionResult<Yoyo>(y, RepositoryActionStatus.Created); } else { return new RepositoryActionResult<Yoyo>(y, RepositoryActionStatus.NothingModified, null); } } catch (Exception ex) { return new RepositoryActionResult<Yoyo>(null, RepositoryActionStatus.Error, ex); } }