public Advert Add(Advert advert) { using (var context = new AdvertsEntities()) { context.Adverts.Add(advert); context.SaveChanges(); } return(GetById(advert.Id)); }
public User Add(User user) { using (var context = new AdvertsEntities()) { context.Users.Add(user); context.SaveChanges(); } return(GetById(user.Id)); }
public void Delete(int advertId) { using (var context = new AdvertsEntities()) { var item = context.Adverts.FirstOrDefault(s => s.Id == advertId); if (item == null) { throw new NullReferenceException($"Advert {advertId} not found"); } context.Adverts.Remove(item); context.SaveChanges(); } }
public Advert Update(Advert advert) { using (var context = new AdvertsEntities()) { var item = context.Adverts.Where(s => s.Id == advert.Id).FirstOrDefault(); if (item == null) { throw new NullReferenceException($"Advert {advert.Id} not found"); } context.Entry(item).CurrentValues.SetValues(advert); context.SaveChanges(); } return(advert); }
public User Update(User user) { using (var context = new AdvertsEntities()) { var item = context.Users.FirstOrDefault(s => s.Id == user.Id); if (item == null) { throw new NullReferenceException($"User {user.Id} not found"); } context.Entry(item).CurrentValues.SetValues(user); context.SaveChanges(); } return(user); }
public void Delete(int advertId) { var item = GetById(advertId); if (item == null) { throw new NullReferenceException($"Advert {advertId} not found"); } using (var context = new AdvertsEntities()) { context.Adverts.Remove(item); context.SaveChanges(); } }