public ActionResult DeleteConfirm(int id) { AdvertisementsCreateModel adv = DB.GetOneAd(id); if (adv.CreatorId != User.Identity.GetUserId()) { return(View("Error")); } else { DB.DeleteAdv(adv); List <AdvertisementsCreateModel> list = DB.GetCreatorsAds(adv.CreatorId).ToList(); return(View("YourAds", list)); } }
public ActionResult Create(AdvertisementsCreateModel adv) { if (ModelState.IsValid) { adv.CreatorId = User.Identity.GetUserId(); DB.CreateAdv(adv); List <AdvertisementsCreateModel> list = DB.GetCreatorsAds(adv.CreatorId).ToList(); return(View("YourAds", list)); } else { return(View("Create", adv)); } }
public void DeleteAdv(AdvertisementsCreateModel adv) { MySqlCommand command = new MySqlCommand { CommandText = String.Format("DELETE FROM advertisements WHERE AdvId = '{0}'", adv.AdvId), Connection = Connection }; using (Connection) { if (Connection.State != System.Data.ConnectionState.Open) { Connection.Open(); } command.ExecuteNonQuery(); } }
public void UpdateAd(AdvertisementsCreateModel adv) { MySqlCommand command = new MySqlCommand { CommandText = String.Format("UPDATE advertisements SET Type = '{0}', Description = '{1}', Price = '{2}' WHERE AdvId = '{3}'", adv.Type, adv.Description, adv.Price, adv.AdvId), Connection = Connection }; using (Connection) { if (Connection.State != System.Data.ConnectionState.Open) { Connection.Open(); } command.ExecuteNonQuery(); } }
public void CreateAdv(AdvertisementsCreateModel adv) { MySqlCommand command = new MySqlCommand { CommandText = String.Format("INSERT INTO advertisements (CreatorId, Type, Description, Price) VALUES('{0}','{1}','{2}','{3}')", adv.CreatorId, adv.Type, adv.Description, adv.Price), Connection = Connection }; using (Connection) { if (Connection.State != System.Data.ConnectionState.Open) { Connection.Open(); } command.ExecuteNonQuery(); } }
public IEnumerable <AdvertisementsCreateModel> GetCreatorsAds(string creatorId) { List <AdvertisementsCreateModel> ads = new List <AdvertisementsCreateModel>(); MySqlCommand command = new MySqlCommand { CommandText = String.Format("SELECT * FROM advertisements WHERE CreatorId = '{0}'", creatorId), Connection = Connection }; using (Connection) { if (Connection.State != System.Data.ConnectionState.Open) { Connection.Open(); } using (MySqlDataReader reader = command.ExecuteReader()) { if (reader.HasRows) { while (reader.Read()) { AdvertisementsCreateModel adv = new AdvertisementsCreateModel(); adv.CreatorId = reader.GetString(0); adv.AdvId = reader.GetUInt32(1); adv.Type = reader.GetString(2); adv.Description = reader.GetString(3); adv.Price = reader.GetInt32(4); ads.Add(adv); } reader.Close(); return(ads); } else { reader.Close(); throw new Exception("User not created any advertisements!"); } } } }
public AdvertisementsCreateModel GetOneAd(int id) { AdvertisementsCreateModel adv = new AdvertisementsCreateModel(); MySqlCommand command = new MySqlCommand { CommandText = String.Format("SELECT * FROM advertisements WHERE AdvId = '{0}'", id), Connection = Connection }; using (Connection) { if (Connection.State != System.Data.ConnectionState.Open) { Connection.Open(); } using (MySqlDataReader reader = command.ExecuteReader()) { if (reader.HasRows) { while (reader.Read()) { adv.CreatorId = reader.GetString(0); adv.AdvId = reader.GetUInt32(1); adv.Type = reader.GetString(2); adv.Description = reader.GetString(3); adv.Price = reader.GetInt32(4); } return(adv); } else { throw new Exception("No such advertisement in database!"); } } } }