diff --git a/WebApi/EntityManagers/AnswerEntityManager.cs b/WebApi/EntityManagers/AnswerEntityManager.cs index 718e148..30f75d6 100644 --- a/WebApi/EntityManagers/AnswerEntityManager.cs +++ b/WebApi/EntityManagers/AnswerEntityManager.cs @@ -41,35 +41,43 @@ namespace EntityManagers public async Task<(int nbPages, IEnumerable? answers)> getAnswers(int page, int count, AnswerOrderCriteria orderCriteria = AnswerOrderCriteria.ById) { int nbEl = getNbAnswers(); - if (page <= 0 || count <= 0 || page > nbEl / count) + float nbPageFloat = count == 0 ? -1 : (float)nbEl / count; + int nbPages = (int)nbPageFloat; + if (nbPages != nbPageFloat) nbPages++; + + if (page <= 0 || count <= 0 || (page-1)*count >= nbEl) { return await Task.FromResult<( int nbPages, IEnumerable? answers )> (( - count == 0 ? -1 : nbEl / count, + nbPages, null )); } var tmp = dbContext.Answers; + IQueryable tmp2; switch (orderCriteria) { case AnswerOrderCriteria.ById: - tmp.OrderBy(a => a.Id); + tmp2 = tmp.OrderBy(a => a.Id); break; case AnswerOrderCriteria.ByContent: - tmp.OrderBy(a => a.Content); + tmp2 = tmp.OrderBy(a => a.Content); break; case AnswerOrderCriteria.ByIdQuestion: - tmp.OrderBy(a => a.IdQuestion); + tmp2 = tmp.OrderBy(a => a.IdQuestion); + break; + default: + tmp2 = tmp; break; } return await Task.FromResult<( int nbPages, IEnumerable? answers )> (( - nbEl / count, - tmp.Skip((page - 1) * count).Take(count) + nbPages, + tmp2.Skip((page - 1) * count).Take(count) )); } diff --git a/WebApi/TestEntityManagers/Program.cs b/WebApi/TestEntityManagers/Program.cs index 5b31249..44cdffe 100644 --- a/WebApi/TestEntityManagers/Program.cs +++ b/WebApi/TestEntityManagers/Program.cs @@ -264,46 +264,47 @@ void TestGetAnswer(MyDbContext context, int numTest, int id, AnswerEntity? waiti /// member data for the 'TestGetAnswer' test method /// /// a set of all inline datas for the test method -(int numPage, int count, IEnumerable? waiting, AnswerOrderCriteria? orderCriteria)[] TestGetSomeAnswers_Datas() +(int numPage, int count, IEnumerable? waiting, AnswerOrderCriteria? orderCriteria)[] TestGetSomeAnswers_Datas(MyDbContext context) { - var datas = fakeAnswers.datas; + var datas = context.Answers; + var datas2 = context.Answers.OrderBy(a => a.Content); var count = datas.Count(); // remind that to add answers, we haven't ordered this collection // but we just have done a foreach return [ - // 1 : 0 answer from the page 1 then, -1 answer from this page + // 1/2 : 0 answer from the page 1 then, -1 answer from this page // WF : count < 1 so we got null (1, 0, null, null), (1, -1, null, null), - // 2 : 1 answers from the page 0 then, 1 answers from the page -1 + // 3/4 : 1 answers from the page 0 then, 1 answers from the page -1 // WF : page < 1 so we got null (0, 1, null, null), (-1, 1, null, null), - // 3 : 10 answers from the page 1 + // 5 : 10 answers from the page 1 // WF : the first 10 element of datas (1, 10, datas.Take(10), null), - // 4 : 10 elements from the page 1 order by id + // 6 : 10 elements from the page 1 order by id // WF : the first 10 element of datas order by id (1, 10, datas.OrderBy(e => e.Id).Take(10), AnswerOrderCriteria.ById), - // 5 : 10 elements from the page 1 order by content + // 7 : 10 elements from the page 1 order by content // WF : the first 10 element of datas order by content - (1, 10, datas.OrderBy(e => e.Content).Take(10), AnswerOrderCriteria.ById), - // 6 : repeat 3, 4 and 5 with the page 2 + (1, 10, datas2.Take(10), AnswerOrderCriteria.ByContent), + // 8/9/10 : repeat 3, 4 and 5 with the page 2 // WF : the 10 next answer from each case (2, 10, datas.Skip(10).Take(10), null), - (1, 10, datas.OrderBy(e => e.Id).Skip(10).Take(10), AnswerOrderCriteria.ById), - (1, 10, datas.OrderBy(e => e.Content).Skip(10).Take(10), AnswerOrderCriteria.ByContent), - // 7 : count/4 elements from the page 4 + (2, 10, datas.OrderBy(e => e.Id).Skip(10).Take(10), AnswerOrderCriteria.ById), + (2, 10, datas2.Skip(10).Take(10), AnswerOrderCriteria.ByContent), + // 11 : count/4 elements from the page 4 // WF : the lasts count/4 elements of datas - (4, count / 4, datas.TakeLast(count / 4), null), - // 8 : count/4 elements from the page 5 + (4, count / 4, datas.Skip(3*count/4).Take(count/4), null), + // 12 : count/4 elements from the page 5 // WF : null since num (4) >= count / (count/4) - (5, count / 4, null, null), - // 9 : 10 elements from the page 4 - // WF : since there's only 31 elements in fake datas, - // we got the last element - (4, 10, datas.TakeLast(1), null) + (3, count / 2, null, null), + // 13 : 7 elements from the page 5 + // WF : since there's only 30 elements in fake datas, + // we got the 2 last elements + (5, 7, datas.Skip(28).Take(2), null) ]; } @@ -317,12 +318,13 @@ void TestGetAnswer(MyDbContext context, int numTest, int id, AnswerEntity? waiti /// nothing but a Task void TestGetSomeAnswers(MyDbContext context, int numTest, int num, int count, List? waiting, AnswerOrderCriteria? orderCriteria) { - context.Database.EnsureCreated(); var mgr = new AnswerEntityManager(context); - var nbPages = count == 0 ? -1 : mgr.getNbAnswers() / count; + float nbPages = count == 0 ? -1 : (float)mgr.getNbAnswers() / count; + if (nbPages != (int)nbPages) nbPages++; + (int nbPages, IEnumerable? answers) tmp = orderCriteria == null ? mgr.getAnswers(num, count).Result : mgr.getAnswers(num, count, orderCriteria.Value).Result; - if(tmp.nbPages == nbPages){ + if(tmp.nbPages == (int) nbPages){ Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine($"test {2*numTest} AnswerEntityManager.getSomeAnswers OK"); Console.ResetColor(); @@ -348,10 +350,10 @@ void TestGetSomeAnswers(MyDbContext context, int numTest, int num, int count, Li Console.WriteLine($"test {2*numTest + 1} AnswerEntityManager.getSomeAnswers OK"); Console.ResetColor(); } - else if(waiting.Count() != 0 && tmp.answers.Count() == 0) + else if(waiting.Count() != 0 && tmp.answers.Count() != 0) { var nbFautes = 0; - for (var i = 0; i < count; i++) + for (var i = 0; i < waiting.Count(); i++) { var e1 = waiting!.ElementAt(i); var e2 = tmp.answers!.ElementAt(i); @@ -374,16 +376,19 @@ void TestGetSomeAnswers(MyDbContext context, int numTest, int num, int count, Li else { Console.Write("{ "); - tmp.answers.ToList().ForEach(a => Console.Write("{" + $"id : {a.Id}, content : {a.Content}" + "}, ")); + tmp.answers.ToList().ForEach(a => Console.Write("{" + $"{a.Id}" + "}, ")); // I know that I use sames collections Console.WriteLine("}"); + Console.WriteLine($"([{tmp.answers.Select(a => a.Id).Min()}, {tmp.answers.Select(a => a.Id).Max()}], {tmp.answers.Count()})"); } + Console.WriteLine(); Console.Write("WF : "); if (waiting == null) Console.WriteLine("null"); else { Console.Write("{ "); - waiting.ForEach(a => Console.Write("{" + $"id : {a.Id}, content : {a.Content}" + "}, ")); + waiting.ForEach(a => Console.Write("{" + $"{a.Id}" + "}, ")); Console.WriteLine("}"); + Console.WriteLine($"([{waiting.Select(a => a.Id).Min()}, {waiting.Select(a => a.Id).Max()}], {waiting.Count()})"); } } } @@ -398,16 +403,19 @@ void TestGetSomeAnswers(MyDbContext context, int numTest, int num, int count, Li else { Console.Write("{ "); - tmp.answers.ToList().ForEach(a => Console.Write("{" + $"id : {a.Id}, content : {a.Content}" + "}, ")); + tmp.answers.ToList().ForEach(a => Console.Write("{" + $"{a.Id}" + "}, ")); Console.WriteLine("}"); + Console.WriteLine($"([{tmp.answers.Select(a => a.Id).Min()}, {tmp.answers.Select(a => a.Id).Max()}], {tmp.answers.Count()})"); } + Console.WriteLine(); Console.Write("WF : "); if (waiting == null) Console.WriteLine("null"); else { Console.Write("{ "); - waiting.ForEach(a => Console.Write("{" + $"id : {a.Id}, content : {a.Content}" + "}, ")); + waiting.ForEach(a => Console.Write("{" + $"{a.Id}" + "}, ")); Console.WriteLine("}"); + Console.WriteLine($"([{waiting.Select(a => a.Id).Min()}, {waiting.Select(a => a.Id).Max()}], {waiting.Count()})"); } } } @@ -422,16 +430,19 @@ void TestGetSomeAnswers(MyDbContext context, int numTest, int num, int count, Li else { Console.Write("{ "); - tmp.answers.ToList().ForEach(a => Console.Write("{" + $"id : {a.Id}, content : {a.Content}" + "}, ")); + tmp.answers.ToList().ForEach(a => Console.Write("{" + $"{a.Id}" + "}, ")); Console.WriteLine("}"); + Console.WriteLine($"([{tmp.answers.Select(a => a.Id).Min()}, {tmp.answers.Select(a => a.Id).Max()}], {tmp.answers.Count()})"); } + Console.WriteLine(); Console.Write("WF : "); if (waiting == null) Console.WriteLine("null"); else { Console.Write("{ "); - waiting.ForEach(a => Console.Write("{" + $"id : {a.Id}, content : {a.Content}" + "}, ")); + waiting.ForEach(a => Console.Write("{" + $"{a.Id}" + "}, ")); Console.WriteLine("}"); + Console.WriteLine($"([{waiting.Select(a => a.Id).Min()}, {waiting.Select(a => a.Id).Max()}], {waiting.Count()})"); } } } @@ -576,7 +587,7 @@ void TestAnswer(MyDbContext context) } } { - var tmp = TestGetSomeAnswers_Datas(); + var tmp = TestGetSomeAnswers_Datas(context); for (int i = 0; i < tmp.Count(); i++) { TestGetSomeAnswers(