public void GetAssignmentTest() { var options = new DbContextOptionsBuilder <AppDBContext>() .UseInMemoryDatabase(databaseName: "AssignmentGetDatabase") .Options; using (var context = new AppDBContext(options)) { context.Assignment.Add(new Assignment { Id = "1", Name = "1", Description = "1" }); context.Assignment.Add(new Assignment { Id = "2", Name = "2", Description = "2" }); context.Assignment.Add(new Assignment { Id = "3", Name = "3", Description = "3" }); context.Assignment.Add(new Assignment { Id = "4", Name = "4", Description = "4" }); context.SaveChanges(); } using (var context = new AppDBContext(options)) { AssignmentRepository assignmentRepository = new AssignmentRepository(context); Assignment assignment = assignmentRepository.Get("1"); Assert.Equal("1", assignment.Name); } }
public Period Add(Period period) { if (period.StartDate.Date <= DateTime.Now.Date) { throw new ArgumentOutOfRangeException( nameof(period.StartDate), period.StartDate.Date, "Cannot start a period that is less than or equal to the current date."); } if (period.EndDate.Date <= period.StartDate.Date) { throw new ArgumentOutOfRangeException( nameof(period.EndDate), period.StartDate.Date, "Period cannot have an end date that is less than or equal to the start date."); } using (IUnitOfWork unitOfWork = context.CreateUnitOfWork()) { int newPeriodId = periodRepo.Insert(period); if (newPeriodId <= 0) { throw new FailedOperationException("Failed to insert Period.", period); } if (period.IsOpen) { Period oldPeriod = periodRepo .Find(x => x.IsOpen) .FirstOrDefault(y => y.PeriodId != newPeriodId); if (oldPeriod != null) { oldPeriod.IsOpen = false; if (!periodRepo.Update(oldPeriod)) { throw new FailedOperationException("Failed to update Period.", oldPeriod); } } IEnumerable <Assignment> assignments = assignmentRepo.Get(null, null, oldPeriod.PeriodId); foreach (Assignment assignment in assignments) { assignment.PeriodId = newPeriodId; assignment.AttemptCount = 0; assignment.LastAttemptedBy = null; assignment.LastAttemptedDate = DateTime.MinValue; assignment.Status = "Not Called"; assignment.Notes = null; if (assignmentRepo.Insert(assignment) <= 0) { throw new FailedOperationException("Failed to insert Assignment.", assignment); } } } unitOfWork.SaveChanges(); return(periodRepo.GetById(newPeriodId)); } }
public IEnumerable <Assignment> Get(int?agentId, int?repId, int?periodId) { return(assignmentRepo.Get(agentId, repId, periodId)); }