예제 #1
0
        public IEnumerable <Publication> GetOfBuyer(int idBuyer, int status = 0)
        {
            using (CompraAppContext db = new CompraAppContext())
            {
                var query =
                    from p in db.Publications
                    select new
                {
                    Publication = p,
                    OffersCount = db.Offers.Where(o => o.State == (int)Offer.ACTION.OPEN).Count(o => o.IdPublication == p.Id)
                };

                List <Publication> publications = new List <Publication>();
                foreach (var row in query)
                {
                    Publication p = row.Publication;
                    p.CountOffers = row.OffersCount;
                    if ((p.IdBuyer == idBuyer) && (p.State == status))
                    {
                        publications.Add(p);
                    }
                }

                //return db.Publications.Where(p => p.IdBuyer == idBuyer).ToList();
                return(publications);
            }
        }
예제 #2
0
 public Offer Get(int id)
 {
     using (CompraAppContext db = new CompraAppContext())
     {
         return(db.Offers.Find(id));
     }
 }
예제 #3
0
 public Offer MakeAction(int id, Offer.ACTION action)
 {
     using (CompraAppContext db = new CompraAppContext())
     {
         Offer offerForUpdate = db.Offers.Find(id);
         if (Exists(offerForUpdate))
         {
             if (checkAction(offerForUpdate, action))
             {
                 if (action == Offer.ACTION.ACCEPTED)
                 {
                     setRejectedToOffersLosers(db, offerForUpdate);
                     closePublicationByAcceptedOffer(db, offerForUpdate);
                 }
                 offerForUpdate.State = (int)action;
                 db.Offers.Attach(offerForUpdate);
                 db.Entry(offerForUpdate).State = EntityState.Modified;
                 db.SaveChanges();
                 return(offerForUpdate);
             }
             else
             {
                 throw new Exception("Error: Offerta ya procesada como aceptada, cancelada o rechazada.");
             }
         }
         else
         {
             throw new Exception("Error: No existe la oferta.");
         }
     }
 }
예제 #4
0
 public IEnumerable <Offer> GetAll()
 {
     using (CompraAppContext db = new CompraAppContext())
     {
         return(db.Offers.ToList());
     }
 }
예제 #5
0
 public Publication Get(int id)
 {
     using (CompraAppContext db = new CompraAppContext())
     {
         return(db.Publications.Find(id));
     }
 }
예제 #6
0
 public IEnumerable <Offer> GetAll(int state = (int)Offer.ACTION.OPEN)
 {
     using (CompraAppContext db = new CompraAppContext())
     {
         return(db.Offers.Where(o => o.State == state).ToList());
     }
 }
예제 #7
0
 public IEnumerable <Offer> GetBySeller(int idSeller)
 {
     using (CompraAppContext db = new CompraAppContext())
     {
         return(db.Offers.Where(o => o.IdSeller == idSeller).ToList());
     }
 }
예제 #8
0
 public Seller Get(string email)
 {
     using (CompraAppContext db = new CompraAppContext())
     {
         return(db.Sellers.Where(s => s.Email == email).FirstOrDefault());
     }
 }
예제 #9
0
 public Buyer Edit(int id, Buyer buyer)
 {
     using (CompraAppContext db = new CompraAppContext())
     {
         try
         {
             Buyer buyerForUpdate = db.Buyers.Find(id);
             if (Exists(buyerForUpdate))
             {
                 buyerForUpdate.Address       = buyer.Address;
                 buyerForUpdate.Email         = buyer.Email;
                 buyerForUpdate.Name          = buyer.Name;
                 buyerForUpdate.Notifications = buyer.Notifications;
                 buyerForUpdate.Phone         = buyer.Phone;
                 buyerForUpdate.Latitud       = buyer.Latitud;
                 buyerForUpdate.Longitud      = buyer.Longitud;
                 db.Buyers.Attach(buyerForUpdate);
                 db.Entry(buyerForUpdate).State = EntityState.Modified;
                 db.SaveChanges();
                 return(buyerForUpdate);
             }
         }catch (Exception e) {
             var a = e;
         }
         return(null);
     }
 }
예제 #10
0
 public object MakeAction(int id, Publication.STATE action)
 {
     using (CompraAppContext db = new CompraAppContext())
     {
         Publication publicationForUpdate = db.Publications.Find(id);
         if (Exists(publicationForUpdate))
         {
             if (checkAction(publicationForUpdate, action))
             {
                 if (action == Publication.STATE.CLOSE_BY_USER)
                 {
                 }
                 publicationForUpdate.State = (int)action;
                 db.Publications.Attach(publicationForUpdate);
                 db.Entry(publicationForUpdate).State = EntityState.Modified;
                 db.SaveChanges();
                 return(publicationForUpdate);
             }
             else
             {
                 throw new Exception("Error: Publicación ya cerrada.");
             }
         }
         else
         {
             throw new Exception("Error: No existe la publicación.");
         }
     }
 }
예제 #11
0
        private void closePublicationByAcceptedOffer(CompraAppContext db, Offer offerForUpdate)
        {
            Publication publication = db.Publications.Find(offerForUpdate.IdPublication);

            publication.State = (int)Publication.STATE.CLOSE_BY_ACEPTED_OFFER;
            db.Publications.Attach(publication);
            db.Entry(publication).State = EntityState.Modified;
        }
예제 #12
0
 public Seller Add(Seller seller)
 {
     using (CompraAppContext db = new CompraAppContext())
     {
         db.Sellers.Add(seller);
         db.SaveChanges();
         return(seller);
     }
 }
예제 #13
0
 public Buyer Add(Buyer buyer)
 {
     using (CompraAppContext db = new CompraAppContext())
     {
         db.Buyers.Add(buyer);
         db.SaveChanges();
         return(buyer);
     }
 }
예제 #14
0
 public Publication Add(Publication publication)
 {
     using (CompraAppContext db = new CompraAppContext())
     {
         publication.Buyer = db.Buyers.Find(publication.IdBuyer);
         db.Buyers.Attach(publication.Buyer);
         db.Publications.Add(publication);
         db.SaveChanges();
         return(publication);
     }
 }
예제 #15
0
        private void setRejectedToOffersLosers(CompraAppContext db, Offer offerWin)
        {
            List <Offer> offersToClose = db.Offers.Where(o => o.IdPublication == offerWin.IdPublication && o.Id != offerWin.Id).ToList();

            foreach (Offer offer in offersToClose)
            {
                offer.State = (int)Offer.ACTION.REJECTED;
                db.Offers.Attach(offer);
                db.Entry(offer).State = EntityState.Modified;
            }
        }
예제 #16
0
        public IEnumerable <Publication> GetAll(int state = (int)Publication.STATE.OPEN)
        {
            using (CompraAppContext db = new CompraAppContext())
            {
                List <Publication> publications = db.Publications.Where(x => x.State == state).Include(b => b.Buyer).ToList();

                foreach (Publication publication in publications)
                {
                    publication.NameBuyer = publication.Buyer.Name;
                }

                return(publications);
            }
        }
예제 #17
0
        public Offer Add(Offer offer)
        {
            using (CompraAppContext db = new CompraAppContext())
            {
                offer.Publication = db.Publications.Find(offer.IdPublication);
                db.Publications.Attach(offer.Publication);

                offer.Seller = db.Sellers.Find(offer.IdSeller);
                db.Sellers.Attach(offer.Seller);

                db.Offers.Add(offer);
                db.SaveChanges();
                return(offer);
            }
        }
예제 #18
0
        public IEnumerable <Offer> GetToABuyerByStatus(int idBuyer, int status = 0)
        {
            using (CompraAppContext db = new CompraAppContext())
            {
                List <Offer> query = (from o in db.Offers
                                      join p in db.Publications on o.IdPublication equals p.Id
                                      where o.State == status
                                      select o).ToList();


                foreach (Offer o in query)
                {
                    o.Seller = db.Sellers.Find(o.IdSeller);
                }
                return(query);
            }
        }
예제 #19
0
        public IEnumerable <Offer> GetOffers(int idPublication, int state = 0)
        {
            using (CompraAppContext db = new CompraAppContext())
            {
                db.Configuration.LazyLoadingEnabled   = false;
                db.Configuration.ProxyCreationEnabled = false;

                var offers = db.Offers.Where(o => o.State == state).Where(o => o.IdPublication == idPublication).ToList();

                foreach (Offer o in offers)
                {
                    o.Seller = db.Sellers.Find(o.IdSeller);
                }

                return(offers);
            }
        }
예제 #20
0
 public bool Delete(int id)
 {
     using (CompraAppContext db = new CompraAppContext())
     {
         try
         {
             Publication publication = db.Publications.Find(id);
             db.Publications.Remove(publication);
             db.SaveChanges();
             return(true);
         }
         catch (Exception ex)
         {
             return(false);
         }
     }
 }
예제 #21
0
 public bool Delete(int id)
 {
     using (CompraAppContext db = new CompraAppContext())
     {
         try
         {
             Offer offer = db.Offers.Find(id);
             db.Offers.Remove(offer);
             db.SaveChanges();
             return(true);
         }
         catch (Exception ex)
         {
             return(false);
         }
     }
 }
예제 #22
0
        public Publication Edit(int id, Publication publication)
        {
            using (CompraAppContext db = new CompraAppContext())
            {
                Publication publicationForUpdate = db.Publications.Find(id);
                if (Exists(publicationForUpdate))
                {
                    publicationForUpdate.IdBuyer      = publication.IdBuyer;
                    publicationForUpdate.Price        = publication.Price;
                    publicationForUpdate.PriceMaxItem = publication.PriceMaxItem;
                    publicationForUpdate.PriceMinItem = publication.PriceMinItem;
                    publicationForUpdate.State        = publication.State;
                    publicationForUpdate.StateItem    = publication.StateItem;
                    db.Publications.Attach(publicationForUpdate);
                    db.Entry(publicationForUpdate).State = EntityState.Modified;
                    db.SaveChanges();
                }

                return(publicationForUpdate);
            }
        }
예제 #23
0
        public Seller Edit(int id, Seller seller)
        {
            using (CompraAppContext db = new CompraAppContext())
            {
                Seller sellerForUpdate = db.Sellers.Find(id);
                if (Exists(sellerForUpdate))
                {
                    sellerForUpdate.Address       = seller.Address;
                    sellerForUpdate.Email         = seller.Email;
                    sellerForUpdate.Name          = seller.Name;
                    sellerForUpdate.Latitud       = seller.Latitud;
                    sellerForUpdate.Longitud      = seller.Longitud;
                    sellerForUpdate.Notifications = seller.Notifications;
                    sellerForUpdate.Phone         = seller.Phone;
                    db.Sellers.Attach(sellerForUpdate);
                    db.Entry(sellerForUpdate).State = EntityState.Modified;
                    db.SaveChanges();
                }

                return(sellerForUpdate);
            }
        }
예제 #24
0
        public Offer Edit(int id, Offer offer)
        {
            using (CompraAppContext db = new CompraAppContext())
            {
                Offer offerForUpdate = db.Offers.Find(id);
                if (Exists(offerForUpdate))
                {
                    offerForUpdate.DeliveryItem     = offer.DeliveryItem;
                    offerForUpdate.DeliveryZoneItem = offer.DeliveryZoneItem;
                    offerForUpdate.DescriptionItem  = offer.DescriptionItem;
                    offerForUpdate.IdPublication    = offer.IdPublication;
                    offerForUpdate.IdSeller         = offer.IdSeller;
                    offerForUpdate.PhotoItem        = offer.PhotoItem;
                    offerForUpdate.PriceItem        = offer.PriceItem;
                    offerForUpdate.State            = offer.State;
                    offerForUpdate.StateItem        = offer.StateItem;
                    db.Offers.Attach(offerForUpdate);
                    db.Entry(offerForUpdate).State = EntityState.Modified;
                    db.SaveChanges();
                }

                return(offerForUpdate);
            }
        }