public string NonPossiedo(string tokenAnnuncio, string tokenAnnuncioUtente) { try { using (DatabaseContext db = new DatabaseContext()) { db.Database.Connection.Open(); using (var transazione = db.Database.BeginTransaction()) { if (annullaVenditaSuDatabase(db, tokenAnnuncioUtente)) { transazione.Commit(); int idUtente = (Session["utente"] as PersonaModel).Persona.ID; Guid tokenGuid = getTokenDecodificato(tokenAnnuncio); ANNUNCIO model = db.ANNUNCIO.FirstOrDefault(m => m.TOKEN == tokenGuid); AnnuncioViewModel viewModelAnnuncio = new AnnuncioViewModel(db, model); return(RenderRazorViewToString("PartialPages/_Possiedo", viewModelAnnuncio)); } } } } catch (Exception ex) { //Elmah.ErrorSignal.FromCurrentContext().Raise(ex); LoggatoreModel.Errore(ex); } // annuncio non in possesso throw new Exception(ExceptionMessage.YouDontHaveThisAd); }
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()); }
public ActionResult Completato(int id) { // se ancora la registrazione è incompleta, lo obbligo a concluderla if (!Utility.IsUtenteAttivo(1, TempData)) { return(RedirectToAction("Impostazioni", "Utente")); } AnnuncioViewModel viewModel = null; try { ANNUNCIO model = new ANNUNCIO(); using (DatabaseContext db = new DatabaseContext()) { model = db.ANNUNCIO.Include(m => m.ANNUNCIO_FOTO) .Include(m => m.CATEGORIA) //.Include("ANNUNCIO_FOTO.FOTO") .SingleOrDefault(m => m.ID == id); viewModel = new AnnuncioViewModel(db, model); if (model.CONDIVISIONE_FACEBOOK_G4G == null || (StatoPubblicaAnnuncioFacebook)model.CONDIVISIONE_FACEBOOK_G4G == StatoPubblicaAnnuncioFacebook.NonPubblicato) { // PUBBLICAZIONE SOCIAL string emailUtente = (Session["utente"] as PersonaModel) .Email.FirstOrDefault(item => item.TIPO == (int)TipoEmail.Registrazione).EMAIL; string dominio = GetCurrentDomain(); if (HttpContext.IsDebuggingEnabled) { dominio = "https://www.gratisforgratis.com"; } string indirizzoImmagine = dominio + "/Uploads/Images/" + emailUtente + "/" + DateTime.Now.Year + "/Normal/" + viewModel.Foto[0].ALLEGATO.NOME; SendPostFacebook(viewModel.Nome, viewModel.Nome + " GRATIS con " + viewModel.Punti, indirizzoImmagine, dominio); model.CONDIVISIONE_FACEBOOK_G4G = (int)StatoPubblicaAnnuncioFacebook.Pubblicato; db.SaveChanges(); } } } catch (Exception ex) { //ErrorSignal.FromCurrentContext().Raise(ex); LoggatoreModel.Errore(ex); return(RedirectToAction("Index", "Utente")); } return(View(viewModel)); }
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)); }
public string Desidero(string token) { using (DatabaseContext db = new DatabaseContext()) { int idUtente = (Session["utente"] as PersonaModel).Persona.ID; db.Database.Connection.Open(); Guid tokenGuid = getTokenDecodificato(token); AnnuncioViewModel annuncio = null; if (addDesiderio(db, tokenGuid, idUtente, ref annuncio)) { //AnnuncioViewModel viewModelAnnuncio = new AnnuncioViewModel(db, annuncio); annuncio.Desidero = true; return(RenderRazorViewToString("PartialPages/_Desidero", annuncio)); } } // desiderio non registrato throw new Exception(ExceptionMessage.WishNotSave); }
public string NonDesidero(string token) { using (DatabaseContext db = new DatabaseContext()) { int idUtente = (Session["utente"] as PersonaModel).Persona.ID; db.Database.Connection.Open(); Guid tokenGuid = getTokenDecodificato(token); IQueryable <ANNUNCIO_DESIDERATO> model = db.ANNUNCIO_DESIDERATO.Where(m => m.ANNUNCIO.TOKEN == tokenGuid && m.ID_PERSONA == idUtente); AnnuncioViewModel viewModelAnnuncio = new AnnuncioViewModel(db, model.FirstOrDefault().ANNUNCIO); viewModelAnnuncio.Desidero = false; db.ANNUNCIO_DESIDERATO.RemoveRange(model); if (db.SaveChanges() > 0) { return(RenderRazorViewToString("PartialPages/_Desidero", viewModelAnnuncio)); } } // desiderio non registrato throw new Exception(ExceptionMessage.NotWishNotSave); }
private bool addDesiderio(DatabaseContext db, Guid tokenGuid, int idUtente, ref AnnuncioViewModel annuncio) { ANNUNCIO_DESIDERATO model = db.ANNUNCIO_DESIDERATO.Where(m => m.ANNUNCIO.TOKEN == tokenGuid && m.ID_PERSONA == idUtente) .FirstOrDefault(); if (model == null) { var modelAnnuncio = db.ANNUNCIO.SingleOrDefault(m => m.TOKEN == tokenGuid && m.ID_PERSONA != idUtente); annuncio = new AnnuncioViewModel(db, modelAnnuncio); if (annuncio != null) { // inserisco l'annuncio tra quelli desiderati model = new ANNUNCIO_DESIDERATO(); model.ID_ANNUNCIO = annuncio.Id; model.ID_PERSONA = idUtente; model.STATO = (int)Stato.ATTIVO; db.ANNUNCIO_DESIDERATO.Add(model); return(db.SaveChanges() > 0); } } return(false); }
public ActionResult InviaOfferta(OffertaViewModel viewModel) { AnnuncioModel model = new AnnuncioModel(); string messaggio = ErrorResource.BidAd; if (ModelState.IsValid) { if (!Utility.IsUtenteAttivo(1, TempData)) { Response.StatusCode = (int)System.Net.HttpStatusCode.BadRequest; return(Json(ErrorResource.UserEnabled)); } using (DatabaseContext db = new DatabaseContext()) { using (DbContextTransaction transaction = db.Database.BeginTransaction()) { try { if (viewModel.Save(db, ref messaggio)) { // aggiungere nella lista dei desideri string tokenDecodificato = HttpContext.Server.UrlDecode(viewModel.Annuncio.Token); Guid tokenGuid = Guid.Parse(tokenDecodificato); PersonaModel utente = (PersonaModel)HttpContext.Session["utente"]; AnnuncioViewModel annuncio = null; addDesiderio(db, tokenGuid, utente.Persona.ID, ref annuncio); // salvare transazione transaction.Commit(); this.RefreshPunteggioUtente(db); // invia e-mail al venditore if (annuncio.Venditore.Persona != null) { this.SendNotifica(utente.Persona, annuncio.Venditore.Persona, TipoNotifica.OffertaRicevuta, ControllerContext, "offerta", viewModel); } return(Json(new { Messaggio = Language.JsonSendBid })); } transaction.Rollback(); } catch (Exception eccezione) { //Elmah.ErrorSignal.FromCurrentContext().Raise(eccezione); transaction.Rollback(); LoggatoreModel.Errore(eccezione); Response.StatusCode = (int)System.Net.HttpStatusCode.BadRequest; return(Json(eccezione.Message)); } } } } else { var errori = ModelState.Where(m => m.Value.Errors.Count > 0) .Select(m => m.Value.Errors.Select(n => n.ErrorMessage)).SelectMany(m => m).ToList(); if (errori != null && errori.Count > 0) { messaggio = string.Join("<br />", errori); } } // acquisto generico Response.StatusCode = (int)System.Net.HttpStatusCode.BadRequest; return(Json(messaggio)); }
public string CopiaServizio(PubblicaServizioCopiaViewModel viewModel) { if (ModelState.IsValid) { using (DatabaseContext db = new DatabaseContext()) { using (DbContextTransaction transaction = db.Database.BeginTransaction()) { try { PERSONA persona = (Session["utente"] as PersonaModel).Persona; Guid token = Guid.Parse(viewModel.TokenOK); ANNUNCIO annuncio = db.ANNUNCIO.SingleOrDefault(m => m.TOKEN == token && m.ID_PERSONA != persona.ID); if (annuncio != null) { // controllo se ha già quell'annuncio ANNUNCIO annuncioInPossesso = db.ANNUNCIO.FirstOrDefault(m => m.ID_PERSONA == persona.ID && (m.STATO == (int)StatoVendita.ATTIVO || m.STATO == (int)StatoVendita.INATTIVO) && (m.ID == annuncio.ID || m.ID_ORIGINE == annuncio.ID)); if (annuncioInPossesso == null) { ANNUNCIO_DESIDERATO annuncioDesiderato = db.ANNUNCIO_DESIDERATO.Where(m => m.ANNUNCIO.TOKEN == token && m.ID_PERSONA == persona.ID).SingleOrDefault(); if (annuncioDesiderato != null) { db.ANNUNCIO_DESIDERATO.Remove(annuncioDesiderato); db.SaveChanges(); } // copiare e salvare annuncio PubblicazioneViewModel viewModelAnnuncio = UpdateServizio(annuncio, viewModel); // copia foto List <string> fotoEsistenti = annuncio.ANNUNCIO_FOTO .Where(m => viewModelAnnuncio.Foto.Contains(m.ALLEGATO.NOME) == true) .Select(m => m.ALLEGATO.NOME).ToList(); for (int i = 0; i < fotoEsistenti.Count; i++) { string path = "~/Uploads/Images/"; if (annuncio.ATTIVITA != null) { path += annuncio.ATTIVITA.TOKEN; } else { path += annuncio.PERSONA.TOKEN; } string nomeFileOriginale = Server.MapPath(path + "/" + annuncio.DATA_INSERIMENTO.Year + "/Original/" + fotoEsistenti[i]); HttpFile fileOriginale = new HttpFile(nomeFileOriginale); FileUploadifive fileSalvatato = UploadImmagine("/Temp/Images/" + Session.SessionID + "/" + viewModel.TokenUploadFoto, fileOriginale); if (fileSalvatato != null) { string[] array = viewModelAnnuncio.Foto.ToArray(); int indiceArray = Array.IndexOf(array, fileSalvatato.NomeOriginale); viewModelAnnuncio.Foto[indiceArray] = fileSalvatato.Nome; } } viewModelAnnuncio.DbContext = db; annuncioInPossesso = new ANNUNCIO(); if (viewModelAnnuncio.SalvaAnnuncio(ControllerContext, annuncioInPossesso)) { int?idAnnuncio = annuncioInPossesso.ID; if (idAnnuncio != null) { // ASSEGNAZIONE CREDITI PersonaModel utente = ((PersonaModel)Session["utente"]); viewModelAnnuncio.InviaEmail(ControllerContext, annuncio, utente); decimal numeroCreditiBonus = AddBonus(db, utente, viewModelAnnuncio); TempData["BONUS"] = string.Format(Bonus.YouWin, numeroCreditiBonus, Language.Moneta); annuncioInPossesso.PERSONA = persona; // perchè sennò non riesce a recuperare l'associazione AnnuncioViewModel nuovoAnnuncio = new AnnuncioViewModel(db, annuncioInPossesso); transaction.Commit(); return(RenderRazorViewToString("PartialPages/_Possiedo", nuovoAnnuncio)); } } } } } catch (Exception eccezione) { transaction.Rollback(); //ErrorSignal.FromCurrentContext().Raise(eccezione); LoggatoreModel.Errore(eccezione); } } } } // desiderio non registrato throw new Exception(ExceptionMessage.CopyAdvertising); }