public IEnumerable <ReportTypeBElement> GenerateReportB(DateTime from, DateTime until) { List <ReportTypeBElement> report = new List <ReportTypeBElement>(); List <Type> differentTypes = new List <Type>(); List <Request> requestsByCondition = (List <Request>)requestRepository.GetAllByCondition(r => r.CreationDate >= from && r.CreationDate <= until).ToList(); if (requestsByCondition.Count != 0) { foreach (Request r in requestsByCondition) { if (!Exists(differentTypes, r.Type)) { differentTypes.Add(r.Type); ReportTypeBElement newReportElement = new ReportTypeBElement(); newReportElement.Amount = 1; newReportElement.Type = r.Type; InsertWithOrder(report, newReportElement); } else { foreach (ReportTypeBElement rBElement in report) { if (rBElement.Type.Id == (r.TypeId)) { rBElement.Amount++; break; } } } } } return(report); }
private void InsertWithOrder(List <ReportTypeBElement> report, ReportTypeBElement newReportElement) { bool wasInserted = false; for (int i = 0; i < report.Count && !wasInserted; i++) { if (report[i].Amount == 1) { if (report[i].Type.CreationDate > newReportElement.Type.CreationDate) { report.Insert(i, newReportElement); wasInserted = true; } } } if (!wasInserted) { report.Add(newReportElement); } }
public void GenerateReportTypeB() { Area oneArea = new Area() { Name = "Limpieza" }; Topic oneTopic = new Topic() { Name = "Limpieza Ciudad", Area = oneArea }; Type oneType = new Type() { Name = "Liempieza Calle", Topic = oneTopic, CreationDate = DateTime.Now }; Type anotherType = new Type() { Name = "Liempieza Cuadra", Topic = oneTopic, CreationDate = DateTime.Now.AddDays(-3) }; Type anotherType2 = new Type() { Name = "Liempieza Barrio", Topic = oneTopic, CreationDate = DateTime.Now.AddDays(-2) }; Request req1 = new Request() { RequestNumber = 1, CreationDate = DateTime.Now, Email = "*****@*****.**", Status = Status.Creada, Type = oneType }; Request req2 = new Request() { Email = "*****@*****.**", RequestNumber = 2, Status = Status.Creada, Type = oneType }; Request req3 = new Request() { Email = "*****@*****.**", RequestNumber = 3, Status = Status.Aceptada, Type = anotherType }; Request req4 = new Request() { Email = "*****@*****.**", RequestNumber = 4, Status = Status.Aceptada, Type = anotherType2 }; ReportTypeBElement repBElem1 = new ReportTypeBElement() { Amount = 2, Type = oneType, }; ReportTypeBElement repBElem2 = new ReportTypeBElement() { Amount = 1, Type = anotherType, }; ReportTypeBElement repBElem3 = new ReportTypeBElement() { Amount = 1, Type = anotherType2, }; List <Request> requests = new List <Request>(); requests.Add(req1); requests.Add(req2); requests.Add(req3); requests.Add(req4); List <ReportTypeBElement> report = new List <ReportTypeBElement>(); report.Add(repBElem1); report.Add(repBElem2); report.Add(repBElem3); var reqRepoMock = new Mock <IRequestRepository>(MockBehavior.Strict); var adminRepoMock = new Mock <IRepository <Admin> >(MockBehavior.Strict); reqRepoMock.Setup(m => m.GetAllByCondition(It.IsAny <Expression <Func <Request, bool> > >())).Returns(requests); var adminLogic = new AdminLogic(adminRepoMock.Object, reqRepoMock.Object); List <ReportTypeBElement> reportGenerated = (List <ReportTypeBElement>)adminLogic.GenerateReportB(DateTime.Now.AddDays(-1), DateTime.Now.AddDays(+1)); reqRepoMock.VerifyAll(); Assert.IsTrue(report.SequenceEqual(reportGenerated)); }