ActionResult Crea(ImmobiliModel immobile) { using (var session = DatabaseUtilities.OpenSession()) { using (var transaction = session.BeginTransaction()) { try { EntityDB <ImmobiliModel> entityDb = new EntityDB <ImmobiliModel>(); string preQuery = "SELECT TOP 1 id FROM Immobili " + " WHERE 1=1 ORDER BY id DESC"; // string preQuery = "SELECT * FROM Immobili " + // " WHERE 1=1 ORDER BY id DESC LIMIT 1"; SqlCommand preCommand = new SqlCommand(preQuery, session, transaction); SqlDataReader executeSqlDataReader = preCommand.ExecuteReader(); if (executeSqlDataReader.HasRows) { while (executeSqlDataReader.Read()) { immobile.Id = long.Parse(executeSqlDataReader["Id"].ToString()) + 1; } } else { immobile.Id = 1; } executeSqlDataReader.Close(); string query = "INSERT INTO Immobili (Id, Nome, Descrizione, Mq, Prezzo, TipoImmobile, Posizione) VALUES (@id, @nome, @descrizione, @mq, @prezzo, @tipoImmobile, @posizione)"; SqlCommand command = new SqlCommand(query, session, transaction); command.Parameters.AddWithValue("@id", immobile.Id); command.Parameters.AddWithValue("@descrizione", immobile.Descrizione); command.Parameters.AddWithValue("@nome", immobile.Nome); if (!string.IsNullOrEmpty(immobile.Mq)) { command.Parameters.AddWithValue("@mq", immobile.Mq); } else { command.Parameters.AddWithValue("@mq", DBNull.Value); } command.Parameters.AddWithValue("@prezzo", immobile.Prezzo); command.Parameters.AddWithValue("@tipoImmobile", immobile.TipoImmobile); command.Parameters.AddWithValue("@posizione", immobile.Posizione); command.ExecuteNonQuery(); transaction.Commit(); var immobiliList = GetImmobiliList(); return(View("Immobili", immobiliList)); } catch (Exception ex) { transaction.Rollback(); throw ex; } } } }
public ActionResult Edit(string id) { using (var session = DatabaseUtilities.OpenSession()) { using (var transaction = session.BeginTransaction()) { try { EntityDB <ImmobiliModel> entityDb = new EntityDB <ImmobiliModel>(); string query = "select * from Immobili WHERE 1=1 AND id=@id"; SqlCommand command = new SqlCommand(query, session, transaction); command.Parameters.AddWithValue("@id", id); SqlDataReader executeSqlDataReader = command.ExecuteReader(); ImmobiliModel immobili = new ImmobiliModel(); while (executeSqlDataReader.Read()) { var immobile = new ImmobiliModel() { Id = long.Parse(executeSqlDataReader["Id"].ToString()), TipoImmobile = executeSqlDataReader["TipoImmobile"].ToString(), Descrizione = executeSqlDataReader["Descrizione"].ToString(), Mq = executeSqlDataReader["Mq"].ToString(), Nome = executeSqlDataReader["Nome"].ToString(), Posizione = executeSqlDataReader["Posizione"].ToString(), Prezzo = executeSqlDataReader["Prezzo"].ToString() }; List <PictureModel> pic = new List <PictureModel>(); string filepath = "~/Images/"; var path = Server.MapPath(filepath + immobile.Id); DirectoryInfo d = new DirectoryInfo(path); if (d.Exists) { foreach (var file in d.GetFiles()) { pic.Add(new PictureModel() { name = file.Name, image = System.IO.File.ReadAllBytes(file.FullName), selected = file.Name.ToLowerInvariant().Contains("copertina") ? true : false }); } immobile.Picture = pic; } immobili = immobile; } executeSqlDataReader.Close(); return(View("ModifyImmobile", immobili)); } catch (Exception ex) { transaction.Rollback(); throw ex; } } } }
ActionResult Update(ImmobiliModel immobile) { using (var session = DatabaseUtilities.OpenSession()) { using (var transaction = session.BeginTransaction()) { try { string query = "Update Immobili set nome=@nome, descrizione=@descrizione, mq=@mq, prezzo=@prezzo, tipoImmobile=@tipoImmobile, posizione=@posizione where id=@id"; SqlCommand command = new SqlCommand(query, session, transaction); command.Parameters.AddWithValue("@id", immobile.Id); command.Parameters.AddWithValue("@descrizione", immobile.Descrizione); command.Parameters.AddWithValue("@nome", immobile.Nome); if (!string.IsNullOrEmpty(immobile.Mq)) { command.Parameters.AddWithValue("@mq", immobile.Mq); } else { command.Parameters.AddWithValue("@mq", DBNull.Value); } command.Parameters.AddWithValue("@prezzo", immobile.Prezzo); command.Parameters.AddWithValue("@tipoImmobile", immobile.TipoImmobile); command.Parameters.AddWithValue("@posizione", immobile.Posizione); command.ExecuteNonQuery(); transaction.Commit(); var immobiliList = GetImmobiliList(); return(View("Immobili", immobiliList)); } catch (Exception ex) { transaction.Rollback(); throw ex; } } } }
private static List <ImmobiliModel> GetImmobiliList() { using (var session = DatabaseUtilities.OpenSession()) { using (var transaction = session.BeginTransaction()) { try { EntityDB <ImmobiliModel> entityDb = new EntityDB <ImmobiliModel>(); string query = "SELECT * FROM Immobili"; SqlCommand command = new SqlCommand(query, session, transaction); SqlDataReader executeSqlDataReader = command.ExecuteReader(); List <ImmobiliModel> immobili = new List <ImmobiliModel>(); while (executeSqlDataReader.Read()) { var immobile = new ImmobiliModel() { Id = long.Parse(executeSqlDataReader["Id"].ToString()), TipoImmobile = executeSqlDataReader["TipoImmobile"].ToString(), Descrizione = executeSqlDataReader["Descrizione"].ToString(), Mq = executeSqlDataReader["Mq"].ToString(), Nome = executeSqlDataReader["Nome"].ToString(), Posizione = executeSqlDataReader["Posizione"].ToString(), Prezzo = executeSqlDataReader["Prezzo"].ToString() }; immobili.Add(immobile); } executeSqlDataReader.Close(); return(immobili); } catch (Exception ex) { transaction.Rollback(); throw ex; } } } }
public ActionResult AggiungiFoto(IEnumerable <HttpPostedFileBase> file, string id) { string filepath = "~/Images/"; var path = Server.MapPath(filepath + id); DirectoryInfo d = new DirectoryInfo(path); if (!d.Exists) { d.Create(); } int i = 0; foreach (var f in file) { if (file != null) { string pic = System.IO.Path.GetFileName(f.FileName); string imgPath = System.IO.Path.Combine(path, pic); f.SaveAs(imgPath); } } using (var session = DatabaseUtilities.OpenSession()) { using (var transaction = session.BeginTransaction()) { try { EntityDB <ImmobiliModel> entityDb = new EntityDB <ImmobiliModel>(); string query = "SELECT * FROM Immobili " + " WHERE id=@id"; SqlCommand command = new SqlCommand(query, session, transaction); command.Parameters.AddWithValue("@id", id); SqlDataReader executeSqlDataReader = command.ExecuteReader(); ImmobiliModel immobili = entityDb.DataReaderMapToList <ImmobiliModel>(executeSqlDataReader); executeSqlDataReader.Close(); if (immobili != null) { List <PictureModel> pic = new List <PictureModel>(); if (d.Exists) { foreach (var f in d.GetFiles()) { pic.Add(new PictureModel() { name = f.Name, image = System.IO.File.ReadAllBytes(f.FullName) }); } immobili.Picture = pic; } return(PartialView("RiepilogoImmobile", immobili)); } return(PartialView("RiepilogoImmobile", new ImmobiliModel())); } catch (Exception ex) { transaction.Rollback(); ModelState.AddModelError(string.Empty, "Server error try after some time."); return(PartialView("RiepilogoImmobile", new ImmobiliModel())); } } } }