// GET: /Business/RecurringShiftEdit/5 public ActionResult RecurringShiftEdit(Guid id) { ShiftTemplateDTO shiftTemplateDTO = GetShiftTemplateDTO(id); if (shiftTemplateDTO == null) { return(HttpNotFound()); } using (EmployeeController empController = new EmployeeController()) ViewBag.BusinessEmployees = empController.GetEmployeesList(shiftTemplateDTO.BusinessLocationId, Session); ViewBag.BusinessId = shiftTemplateDTO.BusinessId; ViewBag.BusinessLocationId = shiftTemplateDTO.BusinessLocationId; var businessLocationDTO = GetBusinessLocation(shiftTemplateDTO.BusinessLocationId); var businessDTO = GetBusiness(shiftTemplateDTO.BusinessId); ViewBag.BusinessRoles = businessDTO.EnabledRoles; ViewBag.BusinessInternalLocations = businessLocationDTO.GetEnabledInternalLocations(); ViewBag.HasInternalLocations = businessDTO.HasMultiInternalLocations; return(PartialView(shiftTemplateDTO)); }
public ActionResult RecurringShiftCreate(ShiftTemplateDTO shiftTemplateDTO) { if ((shiftTemplateDTO.StartTime > shiftTemplateDTO.FinishTime && !shiftTemplateDTO.FinishNextDay) || shiftTemplateDTO.StartTime == shiftTemplateDTO.FinishTime) { ModelState.AddModelError(String.Empty, "Start time must be before the finish time or next day finish must be selected"); } if (ModelState.IsValid) { using (HttpClientWrapper httpClient = new HttpClientWrapper(Session)) { var responseMessage = httpClient.PostAsJsonAsync("api/ShiftTemplateAPI", shiftTemplateDTO).Result; responseMessage.EnsureSuccessStatusCode(); return(RedirectToAction("RecurringShiftIndex", new { businesslocationid = shiftTemplateDTO.BusinessLocationId })); } } else { ViewBag.BusinessLocationId = shiftTemplateDTO.BusinessLocationId; using (EmployeeController empController = new EmployeeController()) ViewBag.BusinessEmployees = empController.GetEmployeesList(shiftTemplateDTO.BusinessLocationId, Session); var busLocDTO = GetBusinessLocation(shiftTemplateDTO.BusinessLocationId); var businessDTO = GetBusiness(busLocDTO.BusinessId); ViewBag.BusinessRoles = businessDTO.EnabledRoles; ViewBag.HasInternalLocations = businessDTO.HasMultiInternalLocations; ViewBag.BusinessInternalLocations = busLocDTO.GetEnabledInternalLocations(); return(PartialView()); } }
public void Put(Guid id, [FromBody] ShiftTemplateDTO shiftTemplateDTO) { if (ModelState.IsValid) { if (ClaimsAuthorization.CheckAccess("Put", "BusinessId", shiftTemplateDTO.BusinessId.ToString())) { var shiftTemplate = MapperFacade.MapperConfiguration.Map <ShiftTemplateDTO, ShiftTemplate>(shiftTemplateDTO, db.ShiftTemplates.Find(shiftTemplateDTO.Id)); //Business rules if (shiftTemplateDTO.StartTime > shiftTemplateDTO.FinishTime && !shiftTemplateDTO.FinishNextDay) { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, "Shift start time must be before end time")); } //Role selected must be applicable to the Employee if (shiftTemplateDTO.EmployeeId != null && shiftTemplateDTO.RoleId.HasValue && db.Employees.Find(shiftTemplateDTO.EmployeeId).Roles.FirstOrDefault(r => r.Id == shiftTemplateDTO.RoleId) == null) { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, "Employee does not have the role specified")); } if (shiftTemplateDTO.InternalLocationId.HasValue) { shiftTemplate.InternalLocation = db.InternalLocations.Find(shiftTemplateDTO.InternalLocationId); } else { db.Entry(shiftTemplate).Reference(r => r.InternalLocation).CurrentValue = null; } if (shiftTemplateDTO.RoleId.HasValue) { shiftTemplate.Role = db.Roles.Find(shiftTemplateDTO.RoleId); } else { db.Entry(shiftTemplate).Reference(r => r.Role).CurrentValue = null; } if (shiftTemplateDTO.EmployeeId.HasValue) { shiftTemplate.Employee = db.Employees.Find(shiftTemplateDTO.EmployeeId); } else { db.Entry(shiftTemplate).Reference(r => r.Employee).CurrentValue = null; } db.Entry(shiftTemplate).State = EntityState.Modified; db.SaveChanges(); } else { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.Unauthorized)); } } else { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } }
private ShiftTemplateDTO GetShiftTemplateDTO(Guid id) { ShiftTemplateDTO shiftTemplateDTO = null; using (HttpClientWrapper httpClient = new HttpClientWrapper(Session)) { Task <String> response = httpClient.GetStringAsync("api/ShiftTemplateAPI/" + id.ToString()); shiftTemplateDTO = Task.Factory.StartNew(() => JsonConvert.DeserializeObject <ShiftTemplateDTO>(response.Result)).Result; } return(shiftTemplateDTO); }
public HttpResponseMessage PostShiftTemplate(ShiftTemplateDTO shiftTemplateDTO) { if (ModelState.IsValid) { if (ClaimsAuthorization.CheckAccess("Put", "BusinessLocationId", shiftTemplateDTO.BusinessLocationId.ToString())) { //Business rules if (shiftTemplateDTO.StartTime > shiftTemplateDTO.FinishTime && !shiftTemplateDTO.FinishNextDay) { return(Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, "Shift start time must be before end time")); } //Role selected must be applicable to the Employee if (shiftTemplateDTO.EmployeeId != null && shiftTemplateDTO.RoleId.HasValue && db.Employees.Find(shiftTemplateDTO.EmployeeId).Roles.FirstOrDefault(r => r.Id == shiftTemplateDTO.RoleId) == null) { return(Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, "Employee does not have the role specified")); } var shiftTemplate = MapperFacade.MapperConfiguration.Map <ShiftTemplateDTO, ShiftTemplate>(shiftTemplateDTO); shiftTemplate.Id = Guid.NewGuid(); //Assign new ID on save. shiftTemplate.Enabled = true; //default to enabled shiftTemplate.BusinessLocation = db.BusinessLocations.Find(shiftTemplateDTO.BusinessLocationId); if (shiftTemplateDTO.InternalLocationId.HasValue) { shiftTemplate.InternalLocation = db.InternalLocations.Find(shiftTemplateDTO.InternalLocationId); } if (shiftTemplateDTO.RoleId.HasValue) { shiftTemplate.Role = db.Roles.Find(shiftTemplateDTO.RoleId); } if (shiftTemplateDTO.EmployeeId.HasValue) { shiftTemplate.Employee = db.Employees.Find(shiftTemplateDTO.EmployeeId); } db.ShiftTemplates.Add(shiftTemplate); db.SaveChanges(); shiftTemplateDTO = MapperFacade.MapperConfiguration.Map <ShiftTemplate, ShiftTemplateDTO>(shiftTemplate); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, shiftTemplateDTO); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = shiftTemplateDTO.Id })); return(response); } else { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.Unauthorized)); } } else { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } }
public void TestPostShiftTemplate() { Guid busId = Guid.Parse("E8B880E3-02F9-4323-B1F5-B0EA63D83E60"); //ID of Bar Rumba ShiftTemplateDTO template = new ShiftTemplateDTO { Id = Guid.Empty, BusinessId = busId, StartTime = TimeSpan.Parse("08:00:00"), FinishTime = TimeSpan.Parse("14:00:00"), Monday = true }; // var shiftTemplateDTOList = shiftTemplateAPIController.GetRecurringShifts(busId); //Assert.IsNotNull(shiftTemplateDTOList); }
public ActionResult RecurringShiftEdit(ShiftTemplateDTO shiftTemplateDTO) { if ((shiftTemplateDTO.StartTime > shiftTemplateDTO.FinishTime && !shiftTemplateDTO.FinishNextDay) || shiftTemplateDTO.StartTime == shiftTemplateDTO.FinishTime) { ModelState.AddModelError(String.Empty, "Start time must be before the finish time or next day finish must be selected"); } if (ModelState.IsValid) { //Replace with updated location using (HttpClientWrapper httpClient = new HttpClientWrapper(Session)) { var responseMessage = httpClient.PutAsJsonAsync("api/ShiftTemplateAPI/" + shiftTemplateDTO.Id.ToString(), shiftTemplateDTO).Result; if (responseMessage.IsSuccessStatusCode) { return(RedirectToAction("RecurringShiftIndex", new { businesslocationid = shiftTemplateDTO.BusinessLocationId })); } else { //If and error occurred add details to model error. var error = JsonConvert.DeserializeObject <System.Web.Http.HttpError>(responseMessage.Content.ReadAsStringAsync().Result); ModelState.AddModelError(String.Empty, error.Message); } } } using (EmployeeController empController = new EmployeeController()) ViewBag.BusinessEmployees = empController.GetEmployeesList(shiftTemplateDTO.BusinessId, Session); ViewBag.BusinessId = shiftTemplateDTO.BusinessId; var businessDTO = GetBusiness(shiftTemplateDTO.BusinessId); var businessLocationDTO = GetBusinessLocation(shiftTemplateDTO.BusinessLocationId); ViewBag.BusinessRoles = businessDTO.EnabledRoles; ViewBag.BusinessInternalLocations = businessLocationDTO.GetEnabledInternalLocations(); ViewBag.HasInternalLocations = businessDTO.HasMultiInternalLocations; return(PartialView(shiftTemplateDTO)); }