public IHttpActionResult DeleteGoing(GoingViewModel deletegoing) { using (db) { var going = db.Going .Where(u => u.IDUser == deletegoing.IDUser && u.IDEvent == deletegoing.IDEvent) .FirstOrDefault(); db.Entry(going).State = System.Data.Entity.EntityState.Deleted; db.SaveChanges(); } return(Ok()); }
// adding/removing going on "going" click public IHttpActionResult PostGoing(GoingViewModel going) { if (!ModelState.IsValid) { return(BadRequest("Invalid data.")); } using (db) { var IsAllreadyGoing = from a in db.Going where a.IDUser == going.IDUser && a.IDEvent == going.IDEvent select a.GoingID; if (IsAllreadyGoing.Any()) { var new_going = db.Going .Where(u => u.IDUser == going.IDUser && u.IDEvent == going.IDEvent) .FirstOrDefault(); db.Entry(new_going).State = System.Data.Entity.EntityState.Deleted; db.SaveChanges(); } else { db.Going.Add(new Going() { IDUser = going.IDUser, IDEvent = going.IDEvent }); } db.SaveChanges(); } return(Ok()); }
public IHttpActionResult PostEvent(EventViewModel idevent) { EventViewModel eventModel = null; bool am_going = true; GoingViewModel am_i_going = new GoingViewModel(); string num_of_going = string.Format("select IDUser from Going where IDEvent = {0}", idevent.EventID); int count = 0; EventViewModel e = new EventViewModel(); using (SqlConnection conn = new SqlConnection("data source=DESKTOP-VKPMS9H;initial catalog=EventPlannerDB;integrated security=True;multipleactiveresultsets=True")) { conn.Open(); using (SqlCommand cmd = new SqlCommand(num_of_going, conn)) { using (var rd = cmd.ExecuteReader()) { while (rd.Read()) { count++; } } } conn.Close(); } using (db) { IList <GoingViewModel> goings = null; goings = db.Going .Where(t => t.IDEvent == idevent.EventID && t.IDUser == idevent.IDUser) .Include("Event") .Select(t => new GoingViewModel() { IDUser = t.IDUser, IDEvent = t.IDEvent }).ToList <GoingViewModel>(); if (!goings.Any()) { am_going = false; } eventModel = db.Event .Include("Location") .Include("Ticket") .Include("Going") .Where(ev => ev.EventID == idevent.EventID) .Select(ev => new EventViewModel() { EventID = ev.EventID, Title = ev.Title, Starting = ev.Starting, Ending = ev.Ending, Ticket = ev.Ticket, Adresse = ev.Adresse, latitude = ev.latitude, longitude = ev.longitude, NumberOfGoing = count, IsLoginUserGoing = am_going, IDUser = ev.IDUser }).FirstOrDefault <EventViewModel>(); } //User user = db.User.Find(id); if (eventModel == null) { return(NotFound()); } return(Ok(eventModel)); }