public async Task AddShiftAddsShift() { var service = new ShiftService(_client, TestDb, TestContainer); var fixture = new Fixture(); var testShift = fixture.Create <NewShift>(); var userId = fixture.Create <string>(); var res = await service.AddShift(userId, testShift); res.Should().NotBeNullOrEmpty(); var container = _client.GetContainer(TestDb, TestContainer); var shiftResponse = await container.ReadItemAsync <Shift>(res, new PartitionKey(userId)); var shift = shiftResponse.Resource; shift.CrewMate.Should().Be(testShift.CrewMate); shift.Date.Should().Be(testShift.Date); shift.Duration.Should().Be(testShift.Duration); shift.Event.Should().Be(testShift.Event); shift.Id.Should().Be(res); shift.Jobs.Should().BeEmpty(); shift.Location.Should().Be(testShift.Location); shift.Role.Should().Be(testShift.Role); shift.UserId.Should().Be(userId); }
public async Task GetAllShiftsGetsAllUsersShifts() { var service = new ShiftService(_client, TestDb, TestContainer); var fixture = new Fixture(); var userId = fixture.Create <string>(); var usersShifts = fixture.Build <Shift>() .With(s => s.UserId, userId) .CreateMany(); var otherShifts = fixture.CreateMany <Shift>(); await _client.CreateDatabaseIfNotExistsAsync(TestDb); await _client.GetDatabase(TestDb).CreateContainerIfNotExistsAsync(TestContainer, "/userId"); foreach (var shift in usersShifts) { await _client.GetContainer(TestDb, TestContainer).CreateItemAsync(shift, new PartitionKey(shift.UserId)); } foreach (var shift in otherShifts) { await _client.GetContainer(TestDb, TestContainer).CreateItemAsync(shift, new PartitionKey(shift.UserId)); } var actual = await service.GetAllShifts(userId); actual.Should().BeEquivalentTo(usersShifts); }
private void SetShiftScheduleList(int?type, bool allowBlank = false) { IShiftService cs = new ShiftService(Settings.Default.db); ShiftSearchModel csm = new ShiftSearchModel(); List <Shift> certType = cs.Search(csm).ToList(); List <SelectListItem> select = new List <SelectListItem>(); if (allowBlank) { select.Add(new SelectListItem { Text = "", Value = "" }); } foreach (var certt in certType) { if (type.HasValue && type.ToString().Equals(certt.id)) { select.Add(new SelectListItem { Text = certt.name, Value = certt.id.ToString(), Selected = true }); } else { select.Add(new SelectListItem { Text = certt.name, Value = certt.id.ToString(), Selected = false }); } } ViewData["shiftList"] = select; }
public void ScheduleEngineerShift_NoMoreEngineers_Error() { var engineers = new List <Engineer> { new Engineer { Name = "1", Id = 1, Shifts = new List <EngineerShift> { new EngineerShift { Date = DateTime.Today } } }, }; Mock <IShiftRepository> mockRepository = new Mock <IShiftRepository>(MockBehavior.Strict); mockRepository.Setup(s => s.FindEngineersAvailableOn(DateTime.Today)).Returns(engineers); IShiftService service = new ShiftService(mockRepository.Object, Utils.ConfigurationTestBuilder.GetConfiguration()); InvalidOperationException ex = Assert.Throws <InvalidOperationException>(() => service.ScheduleEngineerShift(new ShiftRequestModel { Count = 2, StartDate = DateTime.Today })); Assert.NotNull(ex); Assert.Equal("You requested 2 engineers but only 1 is available", ex.Message); mockRepository.Verify(m => m.ScheduleEngineerShift(It.IsAny <List <EngineerShift> >()), Times.Never()); Mapper.Reset(); }
public void ScheduleEngineerShift_Success() { var engineers = new List <Engineer> { new Engineer { Name = "1", Id = 1 }, new Engineer { Name = "2", Id = 2 }, new Engineer { Name = "3", Id = 3 }, new Engineer { Name = "4", Id = 4 }, new Engineer { Name = "5", Id = 5 }, new Engineer { Name = "6", Id = 6 }, new Engineer { Name = "7", Id = 7 }, new Engineer { Name = "8", Id = 8 }, new Engineer { Name = "9", Id = 9 }, new Engineer { Name = "10", Id = 10 }, }; var savedShiftEngineers = new List <EngineerShift> { new EngineerShift { Engineer = engineers[0], Date = DateTime.Today, Duration = 4 }, new EngineerShift { Engineer = engineers[1], Date = DateTime.Today, Duration = 4 } }; Mock <IShiftRepository> mockRepository = new Mock <IShiftRepository>(MockBehavior.Strict); mockRepository.Setup(s => s.FindEngineersAvailableOn(DateTime.Today)).Returns(engineers); mockRepository.Setup(s => s.ScheduleEngineerShift(It.IsAny <List <EngineerShift> >())).Returns(savedShiftEngineers); IShiftService service = new ShiftService(mockRepository.Object, Utils.ConfigurationTestBuilder.GetConfiguration()); var result = service.ScheduleEngineerShift(new ShiftRequestModel { Count = 2, StartDate = DateTime.Today }); Assert.Equal(Mapper.Map <List <EngineerShiftModel> >(savedShiftEngineers), result); mockRepository.Verify(m => m.ScheduleEngineerShift(It.IsAny <List <EngineerShift> >()), Times.Once()); Mapper.Reset(); }
public async Task GetAllShiftsThrowsOnEmptyUser() { var service = new ShiftService(_client, TestDb, TestContainer); var func = new Func <Task>(() => service.GetAllShifts(string.Empty)); await func.Should().ThrowAsync <ArgumentException>(); }
public ActionResult Create() { IShiftService service = new ShiftService(); var Shift = new Shift(); return(View(Shift)); }
public JsonResult Create([Bind(Include = "code,name,startAt,endAt,shiftType,remark")] Shift model) { ResultMessage msg = new ResultMessage(); try { msg = DoValidation(model); if (!msg.Success) { return(Json(msg, JsonRequestBehavior.AllowGet)); } else { IShiftService cs = new ShiftService(Settings.Default.db); bool isSucceed = cs.Create(model); msg.Success = isSucceed; msg.Content = isSucceed ? "" : "添加失败"; return(Json(msg, JsonRequestBehavior.AllowGet)); } } catch (Exception ex) { return(Json(new ResultMessage() { Success = false, Content = ex.Message }, JsonRequestBehavior.AllowGet)); } }
public async Task <ActionResult <List <ShiftAvailabilityDto> > > GetAvailability(int locationId, DateTimeOffset start, DateTimeOffset end) { if (start >= end) { return(BadRequest("Start date was on or after end date.")); } if (end.Subtract(start).TotalDays > 30) { return(BadRequest("End date and start date are more than 30 days apart.")); } if (!PermissionDataFiltersExtensions.HasAccessToLocation(User, Db, locationId)) { return(Forbid()); } var shiftAvailability = await ShiftService.GetShiftAvailability(start, end, locationId : locationId); if (!User.HasPermission(Permission.ViewAllFutureShifts)) { var location = await Db.Location.AsNoTracking().FirstOrDefaultAsync(l => l.Id == locationId); var timezone = location.Timezone; var restrictionDays = int.Parse(Configuration.GetNonEmptyValue("ViewShiftRestrictionDays")); var currentDate = DateTimeOffset.UtcNow.ConvertToTimezone(timezone).DateOnly(); var endDate = currentDate.TranslateDateForDaylightSavings(timezone, restrictionDays + 1); foreach (var sa in shiftAvailability) { sa.Conflicts = sa.Conflicts.WhereToList(c => c.Start < endDate); } } return(Ok(shiftAvailability.Adapt <List <ShiftAvailabilityDto> >())); }
public void ScheduleEngineerShift_ExceedDayShiftsLimit_Error() { var engineers = new List <Engineer> { new Engineer { Name = "1", Id = 1, Shifts = new List <EngineerShift> { new EngineerShift { Date = new DateTime(2017, 12, 12) } } }, new Engineer { Name = "2", Id = 2 }, }; Mock <IShiftRepository> mockRepository = new Mock <IShiftRepository>(MockBehavior.Strict); mockRepository.Setup(s => s.FindEngineersAvailableOn(new DateTime(2017, 12, 12))).Returns(engineers); IShiftService service = new ShiftService(mockRepository.Object, Utils.ConfigurationTestBuilder.GetConfiguration()); InvalidOperationException ex = Assert.Throws <InvalidOperationException>(() => service.ScheduleEngineerShift(new ShiftRequestModel { Count = 2, StartDate = new DateTime(2017, 12, 12) })); Assert.NotNull(ex); Assert.Equal("1: An engineer can do at most one half day shift in a day.", ex.Message); mockRepository.Verify(m => m.ScheduleEngineerShift(It.IsAny <List <EngineerShift> >()), Times.Never()); Mapper.Reset(); }
public async Task UpdateShiftWithWrongUserDoesNotUpdateDatabase() { var service = new ShiftService(_client, TestDb, TestContainer); var fixture = new Fixture(); var userId = fixture.Create <string>(); var testOriginalShift = fixture.Build <Shift>() .With(s => s.UserId, userId).Create(); var testNewShift = fixture.Build <Shift>() .With(s => s.Id, testOriginalShift.Id).Create(); await _client.CreateDatabaseIfNotExistsAsync(TestDb); await _client.GetDatabase(TestDb).CreateContainerIfNotExistsAsync(TestContainer, "/userId"); await _client.GetContainer(TestDb, TestContainer).CreateItemAsync(testOriginalShift, new PartitionKey(userId)); await service.UpdateShift(userId, testNewShift); var shiftResponse = await _client.GetContainer(TestDb, TestContainer).ReadItemAsync <Shift>(testOriginalShift.Id, new PartitionKey(userId)); var actualShift = shiftResponse.Resource; actualShift.Should().BeEquivalentTo(testOriginalShift); }
public ShiftController(ShiftService shiftService, DutyRosterService dutyRosterService, SheriffDbContext db, IConfiguration configuration) { ShiftService = shiftService; DutyRosterService = dutyRosterService; Db = db; Configuration = configuration; }
private ShiftService CreateShiftService() { var userId = Guid.Parse(User.Identity.GetUserId()); var service = new ShiftService(userId); return(service); }
public void ShiftList() { ShiftService service = new ShiftService(); list = service.ShiftList(); dgvShift.DataSource = list; }
public async Task <ActionResult <List <ShiftDto> > > GetShifts(int locationId, DateTimeOffset start, DateTimeOffset end, bool includeDuties = false) { if (!PermissionDataFiltersExtensions.HasAccessToLocation(User, Db, locationId)) { return(Forbid()); } if (!User.HasPermission(Permission.ViewDutyRoster)) { includeDuties = false; } var shifts = await ShiftService.GetShiftsForLocation(locationId, start, end, includeDuties); if (!User.HasPermission(Permission.ViewAllFutureShifts)) { var location = await Db.Location.AsNoTracking().FirstOrDefaultAsync(l => l.Id == locationId); var timezone = location.Timezone; var restrictionDays = int.Parse(Configuration.GetNonEmptyValue("ViewShiftRestrictionDays")); var currentDate = DateTimeOffset.UtcNow.ConvertToTimezone(timezone).DateOnly(); var endDate = currentDate.TranslateDateForDaylightSavings(timezone, restrictionDays + 1); shifts = shifts.WhereToList(s => s.StartDate < endDate); } return(Ok(shifts.Adapt <List <ShiftDto> >())); }
// GET: Shift // Shift/Index public ActionResult Index() { ShiftService service = CreateShiftService(); var model = service.GetShifts(); return(View(model)); }
//如果存在员工排班,则不可删除 public ActionResult Delete(int id, FormCollection collection) { ResultMessage msg = new ResultMessage(); try { IShiftScheduleService shfSi = new ShiftSheduleService(Settings.Default.db); ShiftSchedule shf = shfSi.FindShiftScheduleByShiftId(id); if (null != shf && shf.id > 0) { msg.Success = false; msg.Content = "班次信息正在使用,不能删除!"; return(Json(msg, JsonRequestBehavior.AllowGet)); } else { IShiftService cs = new ShiftService(Settings.Default.db); bool isSucceed = cs.DeleteById(id); msg.Success = isSucceed; msg.Content = isSucceed ? "" : "删除失败"; return(Json(msg, JsonRequestBehavior.AllowGet)); } } catch (Exception ex) { return(Json(new ResultMessage() { Success = false, Content = ex.Message }, JsonRequestBehavior.AllowGet)); } }
public DistributeScheduleController(DistributeScheduleService distributeSchedule, ShiftService shiftService, SheriffDbContext db, IConfiguration configuration) { DistributeScheduleService = distributeSchedule; ShiftService = shiftService; Db = db; Configuration = configuration; }
public ActionResult Edit(int ID) { IShiftService service = new ShiftService();; var shift = service.Getbykey(ID); return(View(shift)); }
public void ScheduleEngineerShiftRange_Success() { var engineers = new List <Engineer> { new Engineer { Name = "1", Id = 1 }, new Engineer { Name = "2", Id = 2 }, }; Mock <IShiftRepository> mockRepository = new Mock <IShiftRepository>(MockBehavior.Strict); mockRepository.Setup(s => s.FindEngineersAvailableOn(It.IsAny <DateTime>())).Returns(engineers); mockRepository.Setup(s => s.ScheduleEngineerShift(It.IsAny <List <EngineerShift> >())).Returns(new List <EngineerShift>()); IShiftService service = new ShiftService(mockRepository.Object, Utils.ConfigurationTestBuilder.GetConfiguration()); service.ScheduleEngineerShiftRange(new ShiftRequestModel { StartDate = new DateTime(2017, 12, 20), EndDate = new DateTime(2017, 12, 26), Count = 2 }); for (DateTime date = new DateTime(2017, 12, 20); date <= new DateTime(2017, 12, 26); date = date.NextBusinessDay()) { mockRepository.Verify(m => m.FindEngineersAvailableOn(date), Times.Once()); } mockRepository.Verify(m => m.ScheduleEngineerShift(It.IsAny <List <EngineerShift> >()), Times.Exactly(5)); Mapper.Reset(); }
public ActionResult Details(int id) { IShiftService service = new ShiftService();; var Shift = service.Getbykey(id); return(View(Shift)); }
public SheriffController(SheriffService sheriffService, DutyRosterService dutyRosterService, ShiftService shiftService, UserService userUserService, IConfiguration configuration, SheriffDbContext db) : base(userUserService) { SheriffService = sheriffService; ShiftService = shiftService; DutyRosterService = dutyRosterService; Db = db; _uploadPhotoSizeLimitKB = Convert.ToInt32(configuration.GetNonEmptyValue("UploadPhotoSizeLimitKB")); }
// GET: Shift/Delete/5 public ActionResult Delete(int id) { IShiftService cs = new ShiftService(Settings.Default.db); Shift cp = cs.FindById(id); SetDropDownList(cp); return(View(cp)); }
// GET: Shift/Edit/5 public ActionResult Edit(int id) { IShiftService cs = new ShiftService(Settings.Default.db); Shift jt = cs.FindById(id); SetDropDownList(jt); return(View(jt)); }
private void newBtns1_btnDelete_Event(object sender, EventArgs e) { if (MessageBox.Show(Properties.Resources.msgDelete, "삭제 확인", MessageBoxButtons.YesNo) == DialogResult.Yes) { ShiftService service = new ShiftService(); service.DeleteShift(vo.shift_id); ShiftList(); } }
public async Task <ActionResult <List <ShiftAvailabilityDto> > > GetDistributeScheduleForLocation(int locationId, DateTimeOffset start, DateTimeOffset end, bool includeWorkSection) { if (start >= end) { return(BadRequest("Start date was on or after end date.")); } if (end.Subtract(start).TotalDays > 30) { return(BadRequest("End date and start date are more than 30 days apart.")); } if (!PermissionDataFiltersExtensions.HasAccessToLocation(User, Db, locationId)) { return(Forbid()); } if (!User.HasPermission(Permission.ViewDutyRoster)) { includeWorkSection = false; } var shiftAvailability = await ShiftService.GetShiftAvailability(start, end, locationId : locationId); var shiftsWithDuties = await DistributeScheduleService.GetDistributeSchedule(shiftAvailability, includeWorkSection, start, end, locationId); if (!User.HasPermission(Permission.ViewAllFutureShifts) || !User.HasPermission(Permission.ViewDutyRosterInFuture)) { var location = await Db.Location.AsNoTracking().FirstOrDefaultAsync(l => l.Id == locationId); var timezone = location.Timezone; var currentDate = DateTimeOffset.UtcNow.ConvertToTimezone(timezone).DateOnly(); if (!User.HasPermission(Permission.ViewAllFutureShifts)) { var shiftRestrictionDays = int.Parse(Configuration.GetNonEmptyValue("ViewShiftRestrictionDays")); var endDateShift = currentDate.TranslateDateForDaylightSavings(timezone, shiftRestrictionDays + 1); foreach (var sa in shiftsWithDuties) { sa.Conflicts = sa.Conflicts.WhereToList(c => c.Start < endDateShift); } } if (!User.HasPermission(Permission.ViewDutyRosterInFuture)) { var dutyRestrictionHours = float.Parse(Configuration.GetNonEmptyValue("ViewDutyRosterRestrictionHours")); var endDateDuties = currentDate.TranslateDateForDaylightSavings(timezone, hoursToShift: dutyRestrictionHours); foreach (var sa in shiftsWithDuties) { sa.Conflicts.WhereToList(c => c.Start > endDateDuties) .ForEach(c => c.WorkSection = null); } } } return(Ok(shiftsWithDuties.Adapt <List <ShiftAvailabilityDto> >())); }
public ActionResult ShiftIndex() { IShiftService service = new ShiftService(); var list = service.GetAll(); ShiftIndex model = new ShiftIndex(); model.ListShift = list; return(View(model)); }
public void ShouldGetTodayShifts() { var shifts = fixture.GetShiftsForToday(); shiftRepository.Setup(x => x.GetShiftsSince(It.Is <DateTime>(d => DateTime.Compare(d.Date, DateTime.Now.Date) == 0))).Returns(shifts); var todayShifts = new ShiftService(shiftRepository.Object).GetTodaysShifts(); Assert.Equal(todayShifts, shifts); }
public async Task UpdateShiftThrowsOnNullShift() { var service = new ShiftService(_client, TestDb, TestContainer); var fixture = new Fixture(); var userId = fixture.Create <string>(); var func = new Func <Task>(() => service.UpdateShift(userId, null)); await func.Should().ThrowAsync <ArgumentException>(); }
public async Task AddJobThrowsOnEmptyUserId() { var service = new ShiftService(_client, TestDb, TestContainer); var fixture = new Fixture(); var testJob = fixture.Create <NewJob>(); var func = new Func <Task>(() => service.AddJob(string.Empty, testJob)); await func.Should().ThrowAsync <ArgumentException>(); }