public ActionResult Create([Bind(Include = "BusinessId,BusinessLocationId,RoleId,StartTime,FinishTime,FinishNextDay")] ShiftBlockDTO shiftblockdto) { if (shiftblockdto.StartTime > shiftblockdto.FinishTime && !shiftblockdto.FinishNextDay) { 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/ShiftBlockAPI", shiftblockdto).Result; responseMessage.EnsureSuccessStatusCode(); CacheManager.Instance.Remove(CacheManager.CACHE_KEY_BUSINESS_LOCATION + shiftblockdto.BusinessLocationId.ToString()); //Remove the stale business item from the cache CacheManager.Instance.Remove(CacheManager.CACHE_KEY_BUSINESS + shiftblockdto.BusinessId.ToString()); //Remove the stale business item from the cache return(RedirectToAction("Index", new { businesslocationid = shiftblockdto.BusinessLocationId })); } } //Get roles for business using (BusinessController bc = new BusinessController()) { var busLoc = bc.GetBusinessLocation(shiftblockdto.BusinessLocationId, this.Session); ViewBag.BusinessRoles = bc.GetBusinessRoles(busLoc.BusinessId, this.Session); ViewBag.BusinessId = shiftblockdto.BusinessId; ViewBag.BusinessLocationId = shiftblockdto.BusinessLocationId; } return(PartialView()); }
// POST api/<controller> public void Post([FromBody] ShiftBlockDTO shiftBlockDTO) { if (Is <ShiftBlockFeature> .Enabled) { if (ModelState.IsValid) { if (ClaimsAuthorization.CheckAccess("Put", "BusinessLocationId", shiftBlockDTO.BusinessLocationId.ToString())) { //Business rules if (shiftBlockDTO.StartTime > shiftBlockDTO.FinishTime && !shiftBlockDTO.FinishNextDay) { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, "ShiftBlock start time must be before end time")); } var shiftBlock = MapperFacade.MapperConfiguration.Map <ShiftBlockDTO, ShiftBlock>(shiftBlockDTO); shiftBlock.Id = Guid.NewGuid(); //Assign new ID on save. shiftBlock.BusinessLocation = db.BusinessLocations.Find(shiftBlockDTO.BusinessLocationId); shiftBlock.Role = db.Roles.Find(shiftBlockDTO.RoleId); db.ShiftBlocks.Add(shiftBlock); db.SaveChanges(); } else { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.Unauthorized)); } } else { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } } else { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotImplemented)); } }
// PUT api/<controller>/5 public void Put(Guid id, [FromBody] ShiftBlockDTO shiftBlockDTO) { if (Is <ShiftBlockFeature> .Enabled) { if (ModelState.IsValid) { if (ClaimsAuthorization.CheckAccess("Put", "BusinessLocationId", shiftBlockDTO.BusinessLocationId.ToString())) { var shiftBlock = MapperFacade.MapperConfiguration.Map <ShiftBlockDTO, ShiftBlock>(shiftBlockDTO, db.ShiftBlocks.Single(st => st.Id == id)); //Business rules if (shiftBlockDTO.StartTime > shiftBlockDTO.FinishTime && !shiftBlockDTO.FinishNextDay) { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, "ShiftBlock start time must be before end time")); } shiftBlock.Role = db.Roles.Find(shiftBlockDTO.RoleId); db.Entry(shiftBlock).State = EntityState.Modified; db.SaveChanges(); } else { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.Unauthorized)); } } else { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } } else { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotImplemented)); } }
public void TestShiftBlockAPI() { DataModelDbContext db = new DataModelDbContext(); Business business = db.Businesses.FirstOrDefault(b => b.Name == "Bar Rumba"); Role role = db.Roles.FirstOrDefault(r => r.Business.Id == business.Id); Random rnd = new Random(); int rHourStart = rnd.Next(4, 14); //Rand start hour between 4 and 14 int rMinStart = rnd.Next(0, 60); //Rand start hour between 0 and 60 ShiftBlockDTO sbDTO = new ShiftBlockDTO { StartTime = new TimeSpan(rHourStart, rMinStart, 0), FinishTime = new TimeSpan(20, 0, 0), //set finish time of 8pm FinishNextDay = false, BusinessId = business.Id, RoleId = role.Id }; var shiftBlocksbeforeAdd = shiftBlockAPIController.GetShiftBlocks(business.Id); shiftBlockAPIController.Post(sbDTO); var shiftBlocksAfterAdd = shiftBlockAPIController.GetShiftBlocks(business.Id); Assert.IsTrue(shiftBlocksAfterAdd.Count() == (shiftBlocksbeforeAdd.Count() + 1), "Shift block has not been saved correctly"); var obj1 = shiftBlocksAfterAdd.Where(sb => sb.StartTime == new TimeSpan(rHourStart, rMinStart, 0) && sb.FinishTime == new TimeSpan(20, 0, 0)) .FirstOrDefault(); Assert.IsNotNull(obj1); //Test Get single DTO var obj2 = shiftBlockAPIController.Get(obj1.Id); Assert.IsNotNull(obj2); Assert.IsTrue(obj2.StartTime == sbDTO.StartTime && obj2.FinishTime == sbDTO.FinishTime && obj2.FinishNextDay == sbDTO.FinishNextDay && obj2.BusinessId == sbDTO.BusinessId && obj2.RoleId == sbDTO.RoleId, "Objects do not match"); //Test Update obj2.StartTime = new TimeSpan(rHourStart + 1, rMinStart, 0); obj2.FinishTime = new TimeSpan(2, 0, 0); obj2.FinishNextDay = true; shiftBlockAPIController.Put(obj2.Id, obj2); //Test Get again to verify changes DTO var obj3 = shiftBlockAPIController.Get(obj1.Id); Assert.IsTrue(obj3.StartTime == new TimeSpan(rHourStart + 1, rMinStart, 0) && obj2.FinishTime == new TimeSpan(2, 0, 0) && obj2.FinishNextDay == true, "Update did not work"); shiftBlockAPIController.Delete(obj1.Id); var shiftBlocksAfterDelete = shiftBlockAPIController.GetShiftBlocks(business.Id); Assert.IsTrue(shiftBlocksAfterDelete.Count() == shiftBlocksbeforeAdd.Count(), "Shift block has not been deleted correctly"); //Test Get single DTO after delete bool exceptionThrown = false; try { var obj4 = shiftBlockAPIController.Get(obj1.Id); } catch (Exception ex) { exceptionThrown = true; var mg = ex.Message; } Assert.IsTrue(exceptionThrown, "Exception not thrown when getting deleted item"); }
// POST api/shiftapi //Create a new shift public HttpResponseMessage PostShift([FromBody] ShiftDTO shiftDTO, [FromUri] Guid businessLocationId) { if (ClaimsAuthorization.CheckAccess("Put", "BusinessLocationId", businessLocationId.ToString())) { if (ModelState.IsValid) { var dateMinusWeek = shiftDTO.StartDateTime.AddDays(-7); Roster roster = db.Rosters.Find(shiftDTO.RosterId); //Business rules if (roster == null) { return(Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, "Unable to find corresponding roster")); } if (shiftDTO.StartDateTime > shiftDTO.FinishDateTime) { return(Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, "Shift start time must be before end time")); } if (shiftDTO.StartDateTime < roster.WeekStartDate || shiftDTO.StartDateTime >= roster.WeekStartDate.AddDays(7)) { return(Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, "Shift start time must be within the roster week starting '" + roster.WeekStartDate.ToShortDateString() + "'")); } if ((shiftDTO.FinishDateTime - shiftDTO.StartDateTime).Days > 1) { return(Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, "Shift can not span more than one day")); } if (shiftDTO.StartDateTime < WebUI.Common.Common.DateTimeNowLocal()) { return(Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, "Shift start time must not be in the past")); } using (ScheduleActionAPIController scheduleAPIController = new ScheduleActionAPIController()) { var unavailEmployees = scheduleAPIController.GetUnavailableEmployees(shiftDTO.BusinessLocationId.GetValueOrDefault(), shiftDTO.StartDateTime.Ticks.ToString(), shiftDTO.FinishDateTime.Ticks.ToString(), Guid.Empty); if (unavailEmployees.FirstOrDefault(e => e.Id == shiftDTO.EmployeeId) != null) { return(Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, "Employee is not available on this day")); } } var shift = MapperFacade.MapperConfiguration.Map <ShiftDTO, Shift>(shiftDTO); shift.Id = Guid.NewGuid(); //Assign new ID on save. shift.Roster = roster; shift.Employee = db.Employees.Find(shiftDTO.EmployeeId); shift.InternalLocation = db.InternalLocations.Find(shiftDTO.InternalLocationId); shift.Role = db.Roles.Find(shiftDTO.RoleId); db.Shifts.Add(shift); db.SaveChanges(); //If save as shift block also selected, then create new shift block if (shiftDTO.SaveAsShiftBlock) { ShiftBlockDTO shiftBlockDTO = new ShiftBlockDTO { StartTime = shiftDTO.StartTime, FinishTime = shiftDTO.FinishTime, BusinessLocationId = businessLocationId, RoleId = shiftDTO.RoleId, FinishNextDay = (shiftDTO.FinishDay > shiftDTO.StartDay) }; using (ShiftBlockAPIController sb = new ShiftBlockAPIController()) { sb.Post(shiftBlockDTO); } } //HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created); //response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = role.Id })); return(Request.CreateResponse(HttpStatusCode.Created)); } else { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } } else { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.Unauthorized)); } }