예제 #1
0
 public ActionResult Index(string token, string azione = "compra")
 {
     try
     {
         Guid tokenDecriptato = getTokenDecodificato(token);
         using (DatabaseContext db = new DatabaseContext())
         {
             db.Database.Connection.Open();
             AnnuncioModel     model     = new AnnuncioModel();
             AnnuncioViewModel viewModel = model.GetViewModel(db, tokenDecriptato);
             viewModel.Azione = azione;
             if (TempData["esito"] != null)
             {
                 ViewBag.Esito = TempData["esito"];
             }
             else if (TempData["errore"] != null)
             {
                 ModelState.AddModelError("", TempData["errore"] as string);
             }
             return(View(viewModel));
         }
     }
     catch (Exception ex)
     {
         //Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
         LoggatoreModel.Errore(ex);
     }
     return(View());
 }
예제 #2
0
        private void Load(DatabaseContext db, PERSONA persona)
        {
            this.Nome    = persona.NOME;
            this.Cognome = persona.COGNOME;

            this.Foto = persona.PERSONA_FOTO.Select(m => new FotoModel(m.ALLEGATO)).ToList();

            this.listaAcquisti = new List <AnnuncioViewModel>();
            this.listaVendite  = new List <AnnuncioViewModel>();
            this.listaDesideri = new List <AnnuncioViewModel>();
            // far vedere top n acquisti con link
            var query = db.ANNUNCIO.Where(item => item.ID_COMPRATORE == persona.ID &&
                                          item.TRANSAZIONE_ANNUNCIO.Count(m => m.STATO == (int)StatoPagamento.ATTIVO || m.STATO == (int)StatoPagamento.ACCETTATO) > 0 &&
                                          (item.STATO == (int)StatoVendita.VENDUTO || item.STATO == (int)StatoVendita.BARATTATO) &&
                                          (item.ID_OGGETTO != null || item.ID_SERVIZIO != null));
            List <ANNUNCIO> lista = query
                                    .OrderByDescending(item => item.DATA_INSERIMENTO)
                                    .Take(4).ToList();

            foreach (ANNUNCIO m in lista)
            {
                AnnuncioModel annuncioModel = new AnnuncioModel();
                this.listaAcquisti.Add(annuncioModel.GetViewModel(db, m));
            }
            // far vedere vendite recenti con link
            var queryVendite = db.ANNUNCIO.Where(item => item.ID_PERSONA == persona.ID &&
                                                 (item.STATO != (int)StatoVendita.ELIMINATO && item.STATO != (int)StatoVendita.BARATTATO && item.STATO != (int)StatoVendita.VENDUTO) &&
                                                 (item.ID_OGGETTO != null || item.ID_SERVIZIO != null));
            List <ANNUNCIO> listaVendite = queryVendite
                                           .OrderByDescending(item => item.DATA_INSERIMENTO)
                                           .Take(4).ToList();

            foreach (ANNUNCIO m in listaVendite)
            {
                AnnuncioModel annuncioModel = new AnnuncioModel();
                this.listaVendite.Add(annuncioModel.GetViewModel(db, m));
            }
            // far vedere top n desideri con link
            List <ANNUNCIO_DESIDERATO> listaDesideri = db.ANNUNCIO_DESIDERATO
                                                       .Where(item => item.ID_PERSONA == persona.ID && (item.ANNUNCIO.STATO == (int)StatoVendita.INATTIVO ||
                                                                                                        item.ANNUNCIO.STATO == (int)StatoVendita.ATTIVO) && (item.ANNUNCIO.DATA_FINE == null ||
                                                                                                                                                             item.ANNUNCIO.DATA_FINE >= DateTime.Now))
                                                       .OrderByDescending(item => item.ANNUNCIO.DATA_INSERIMENTO)
                                                       .Take(4)
                                                       .ToList();

            listaDesideri.ForEach(m =>
                                  this.listaDesideri.Add(
                                      new AnnuncioViewModel(db, m.ANNUNCIO)
                                      )
                                  );
        }
예제 #3
0
        public ActionResult Index(int pagina = 1)
        {
            List <AnnuncioViewModel> vendite = new List <AnnuncioViewModel>();

            try
            {
                using (DatabaseContext db = new DatabaseContext())
                {
                    int utente = ((PersonaModel)Session["utente"]).Persona.ID;
                    var query  = db.ANNUNCIO.Where(item => item.ID_PERSONA == utente &&
                                                   (item.STATO != (int)StatoVendita.ELIMINATO && item.STATO != (int)StatoVendita.BARATTATO && item.STATO != (int)StatoVendita.VENDUTO) &&
                                                   (item.ID_OGGETTO != null || item.ID_SERVIZIO != null));
                    int numeroElementi = Convert.ToInt32(WebConfigurationManager.AppSettings["numeroElementi"]);
                    ViewData["TotalePagine"] = (int)Math.Ceiling((decimal)query.Count() / (decimal)numeroElementi);
                    ViewData["Pagina"]       = pagina;
                    pagina -= 1;
                    List <ANNUNCIO> listaAnnunci = query
                                                   .OrderByDescending(item => item.DATA_INSERIMENTO)
                                                   .Skip(pagina * numeroElementi)
                                                   .Take(numeroElementi)
                                                   .ToList();

                    foreach (ANNUNCIO annuncio in listaAnnunci)
                    {
                        AnnuncioModel annuncioModel = new AnnuncioModel();
                        // escludo il singolo annuncio in caso di errore
                        try
                        {
                            vendite.Add(annuncioModel.GetViewModel(db, annuncio));
                        }
                        catch (Exception eccezione)
                        {
                            Elmah.ErrorSignal.FromCurrentContext().Raise(eccezione);
                        }
                    }

                    if (vendite.Count > 0)
                    {
                        RefreshPunteggioUtente(db);
                    }
                }
            }
            catch (Exception ex)
            {
                //Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
                LoggatoreModel.Errore(ex);
            }
            return(View(vendite));
        }
예제 #4
0
        public ActionResult Conclusi(int pagina = 1)
        {
            List <AnnuncioViewModel> acquistiConclusi = new List <AnnuncioViewModel>();

            try
            {
                using (DatabaseContext db = new DatabaseContext())
                {
                    int utente = ((PersonaModel)Session["utente"]).Persona.ID;
                    var query  = db.ANNUNCIO.Where(item => item.ID_PERSONA != utente && item.ID_COMPRATORE == utente &&
                                                   (item.STATO == (int)StatoVendita.VENDUTO || item.STATO == (int)StatoVendita.ELIMINATO || item.STATO == (int)StatoVendita.BARATTATO) &&
                                                   (item.ID_OGGETTO != null || item.ID_SERVIZIO != null));
                    int numeroElementi = Convert.ToInt32(WebConfigurationManager.AppSettings["numeroElementi"]);
                    ViewData["TotalePagine"] = (int)Math.Ceiling((decimal)query.Count() / (decimal)numeroElementi);
                    ViewData["Pagina"]       = pagina;
                    pagina -= 1;
                    string          randomString = Utility.RandomString(3);
                    List <ANNUNCIO> lista        = query
                                                   .OrderByDescending(item => item.DATA_VENDITA)
                                                   .Skip(pagina * numeroElementi)
                                                   .Take(numeroElementi).ToList();
                    foreach (ANNUNCIO m in lista)
                    {
                        AnnuncioModel     annuncioModel = new AnnuncioModel();
                        AnnuncioViewModel viewModel     = annuncioModel.GetViewModel(db, m);
                        OFFERTA           offerta       = m.OFFERTA.SingleOrDefault(o => o.STATO == (int)StatoOfferta.ACCETTATA);
                        if (offerta != null)
                        {
                            viewModel.Offerta = new OffertaViewModel(db, offerta);
                        }
                        acquistiConclusi.Add(viewModel);
                    }

                    if (acquistiConclusi.Count > 0)
                    {
                        RefreshPunteggioUtente(db);
                    }
                }
            }
            catch (Exception eccezione)
            {
                //Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
                LoggatoreModel.Errore(eccezione);
            }
            return(View(acquistiConclusi));
        }
예제 #5
0
        public ActionResult Compra(AcquistoViewModel viewModel)
        {
            AnnuncioModel model = null;

            using (DatabaseContext db = new DatabaseContext())
            {
                using (DbContextTransaction transaction = db.Database.BeginTransaction())
                {
                    try
                    {
                        Guid token = getTokenDecodificato(viewModel.Token);
                        model = new AnnuncioModel(token, db);
                        if (model.TOKEN == null)
                        {
                            throw new System.Web.HttpException(404, ExceptionMessage.AdNotFound);
                        }

                        if (ModelState.IsValid)
                        {
                            if (!Utility.IsUtenteAttivo(1, TempData))
                            {
                                ModelState.AddModelError("", ErrorResource.UserEnabled);
                            }
                            else
                            {
                                Models.Enumerators.VerificaAcquisto verifica = model.Acquisto(db, viewModel);
                                if (verifica == Models.Enumerators.VerificaAcquisto.Ok)
                                {
                                    if (model.CompletaAcquisto(db, viewModel))
                                    {
                                        TempData["Esito"] = Language.JsonBuyAd;
                                        TempData["pagamentoEffettuato"] = true;
                                        transaction.Commit();
                                        this.RefreshPunteggioUtente(db);
                                        this.SendNotifica(model.PERSONA, model.PERSONA1, TipoNotifica.AnnuncioAcquistato, ControllerContext, "annuncioAcquistato", model);
                                        this.SendNotifica(model.PERSONA1, model.PERSONA, TipoNotifica.AnnuncioVenduto, ControllerContext, "annuncioVenduto", model);
                                        return(RedirectToAction("Index", "Annuncio", new { token = viewModel.Token }));
                                    }
                                    // altrimenti pagamento fallito!
                                }
                                else if (verifica == Models.Enumerators.VerificaAcquisto.VerificaCartaCredito)
                                {
                                    string actionPagamento = "Payment";
                                    if (viewModel.TipoCarta != TipoCartaCredito.PayPal)
                                    {
                                        actionPagamento = "PaymentWithCreditCard";
                                    }
                                    transaction.Commit();
                                    Session["PayPalCompra"]   = viewModel;
                                    Session["PayPalAnnuncio"] = model;
                                    return(RedirectToAction(actionPagamento, "PayPal", new { Id = model.ID, Token = viewModel.Token, Azione = AzionePayPal.Acquisto }));
                                }
                                else
                                {
                                    Elmah.ErrorSignal.FromCurrentContext().Raise(new Exception(string.Format("Messaggio {0} per l'errore {1}", ErrorResource.AdBuyFailed, verifica.ToString())));
                                    ModelState.AddModelError("", ErrorResource.AdBuyFailed);
                                }
                            }
                        }
                        //transaction.Rollback();
                    }
                    catch (System.Web.HttpException eccezione)
                    {
                        //Elmah.ErrorSignal.FromCurrentContext().Raise(eccezione);
                        LoggatoreModel.Errore(eccezione);
                        throw new System.Web.HttpException(404, eccezione.Message);
                    }
                    catch (Exception eccezione)
                    {
                        ModelState.AddModelError("", eccezione.Message);
                        //Elmah.ErrorSignal.FromCurrentContext().Raise(eccezione);
                        LoggatoreModel.Errore(eccezione);
                    }
                    finally
                    {
                        if (db.Database.CurrentTransaction != null)
                        {
                            transaction.Rollback();
                        }
                    }
                    viewModel.Annuncio        = model.GetViewModel(db);
                    viewModel.Annuncio.Azione = "compra";
                }
            }

            ViewData["acquistoViewModel"] = viewModel;
            return(View("Index", viewModel.Annuncio));
        }