public ActionResult <Calendar> Delete(string orgID, [FromBody] CalendarUser calendarUser) { Calendar calendar = new Calendar(); string sql = string.Format("select * from tlbCalendar WHERE date = '{0}' and orgID={1}", calendarUser.Date, orgID); int capacity; try { SqlConnection connection = new SqlConnection(Params.connetionString); connection.Open(); SqlCommand command = new SqlCommand(sql, connection); SqlDataReader dataReader = command.ExecuteReader(); while (dataReader.Read()) { calendar.EmployeesArriving = dataReader["EmployeesArriving"].ToString().Trim(); calendar.SittingCapacity = dataReader["SittingCapacity"].ToString().Trim(); calendar.Date = dataReader["Date"].ToString().Trim(); calendar.WaitingList = dataReader["WaitingList"].ToString().Trim(); } dataReader.Close(); if (calendar.Date != null) { capacity = int.Parse(calendar.SittingCapacity); if (calendar.EmployeesArriving == null || !calendar.EmployeesArriving.Contains(calendarUser.Id)) { return(NotFound()); } else { calendarUser.UpdateCapacity(connection, ++capacity, orgID); string idToRemove = string.Format("{0};", calendarUser.Id); calendar.EmployeesArriving = calendar.EmployeesArriving.Replace(idToRemove, ""); calendarUser.UpdateArrivingID(connection, calendar.EmployeesArriving, orgID); if (!string.IsNullOrEmpty(calendar.WaitingList)) { calendarUser.UpdateCapacity(connection, --capacity, orgID); string idFromWaitingList = calendar.WaitingList.Split(";")[0]; string idToAdd = string.Format("{0};", idFromWaitingList); calendar.EmployeesArriving += idToAdd; calendarUser.UpdateArrivingID(connection, calendar.EmployeesArriving, orgID); calendar.WaitingList = calendar.WaitingList.Replace(idToAdd, ""); calendarUser.UpdateWaitingList(connection, calendar.WaitingList, orgID); calendarUser.SendWaitingListEmail(connection, idFromWaitingList, calendar.Date, orgID); } } string employeesName = calendarUser.GetComingEmployeesNames(calendar.EmployeesArriving, connection, orgID); command.Dispose(); connection.Close(); calendar.EmployeesArriving = employeesName; return(new OkObjectResult(JsonConvert.SerializeObject(calendar))); } return(BadRequest()); } catch (Exception e) { return(new BadRequestObjectResult(e.Message)); } }
public ActionResult <Calendar> Post(string orgID, [FromBody] CalendarUser calendarUser) { string sql = string.Format("select * from tlbCalendar WHERE date = '{0}' and OrgID={1}", calendarUser.Date, orgID); Calendar calendar = new Calendar(); int capacity; string employeesName = null; try { SqlConnection connection = new SqlConnection(Params.connetionString); connection.Open(); SqlCommand command = new SqlCommand(sql, connection); SqlDataReader dataReader = command.ExecuteReader(); while (dataReader.Read()) { calendar.EmployeesArriving = dataReader["EmployeesArriving"].ToString().Trim(); calendar.SittingCapacity = dataReader["SittingCapacity"].ToString(); calendar.Date = dataReader["Date"].ToString(); calendar.ParkingCapacity = dataReader["ParkingCapacity"].ToString(); } dataReader.Close(); command.Dispose(); if (calendar.Date != null) { capacity = int.Parse(calendar.SittingCapacity); if (capacity == 0) { return(new OkObjectResult("no space")); } employeesName = calendarUser.GetComingEmployeesNames(calendar.EmployeesArriving, connection, orgID); if (calendar.EmployeesArriving == null || !calendar.EmployeesArriving.Contains(calendarUser.Id)) { calendarUser.UpdateCapacity(connection, --capacity, orgID); calendar.EmployeesArriving += string.Format("{0};", calendarUser.Id); calendarUser.UpdateArrivingID(connection, calendar.EmployeesArriving, orgID); employeesName += calendarUser.GetEmployeeName(connection, orgID) + ','; } command.Dispose(); connection.Close(); calendar.EmployeesArriving = employeesName; return(new OkObjectResult(JsonConvert.SerializeObject(calendar))); } else { Office office = new Office(); office.GetOfficeById(orgID); calendar.OrgID = office.ID; calendar.ParkingCapacity = office.ParkingAmount; calendar.SittingCapacity = office.OfficeCapacity; calendar.EmployeesArriving += string.Format("{0};", calendarUser.Id); calendar.InsertDate(); employeesName = calendarUser.GetEmployeeName(connection, orgID) + ','; calendar.EmployeesArriving = employeesName; return(new OkObjectResult(JsonConvert.SerializeObject(calendar))); } } catch (Exception e) { return(new BadRequestObjectResult(e.Message)); } }