Example #1
0
        public Advert Add(Advert advert)
        {
            using (var context = new AdvertsEntities())
            {
                context.Adverts.Add(advert);
                context.SaveChanges();
            }

            return(GetById(advert.Id));
        }
Example #2
0
        public User Add(User user)
        {
            using (var context = new AdvertsEntities())
            {
                context.Users.Add(user);
                context.SaveChanges();
            }

            return(GetById(user.Id));
        }
Example #3
0
 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();
     }
 }
Example #4
0
        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);
        }
Example #5
0
        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);
        }
Example #6
0
        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();
            }
        }