diff --git a/ToDoWebApp/Data/Models/TodoItem.cs b/ToDoWebApp/Data/Models/TodoItem.cs index 8fdd88d..5261b19 100644 --- a/ToDoWebApp/Data/Models/TodoItem.cs +++ b/ToDoWebApp/Data/Models/TodoItem.cs @@ -25,6 +25,7 @@ public string Name } /**/ [DataType(DataType.Text)] + [StringLength(140)] public string? Description { get; @@ -94,7 +95,8 @@ public enum Status [Display(Name = "Done")] Done, [Display(Name = "Archived")] - Archived - + Archived, + [Display(Name = "Planned")] + Planned } } \ No newline at end of file diff --git a/ToDoWebApp/Repository/TodoAPIRepository.cs b/ToDoWebApp/Repository/TodoAPIRepository.cs index d3705cd..0e2881a 100644 --- a/ToDoWebApp/Repository/TodoAPIRepository.cs +++ b/ToDoWebApp/Repository/TodoAPIRepository.cs @@ -16,20 +16,51 @@ public class TodoAPIRepository : ITodoItemAPIRepository public List todoItem { get; set; } public TodoAPIRepository() - { + { Add(new TodoItem { + TodoItemId = 1, Name = "Item1", Description = "Description1", - priority = 1 + priority = 1, + status = Status.Wip, + DeadLineDate = new DateTime(2088, 3, 9, 7, 0, 0) // 3/1/2088 7:00:00 AM }); Add(new TodoItem { + TodoItemId = 2, Name = "Item2", Description = "Description2", - priority = 3 + priority = 2, + status = Status.Wip, + DeadLineDate = new DateTime(2088, 3, 1, 7, 0, 0) // 3/1/2088 7:00:00 AM + }); + Add(new TodoItem + { + TodoItemId = 3, + Name = "ItemForTestAlreadeCreated", + Description = "Description3", + priority = 2, + status = Status.Wip, + DeadLineDate = new DateTime(2088, 3, 3, 7, 0, 0) // 3/1/2088 7:00:00 AM + }); + Add(new TodoItem + { + TodoItemId = 4, + Name = "ItemForTestAlreadeCreate3232d", + Description = "Description3", + priority = 2, + status = Status.Wip, + DeadLineDate = new DateTime(2088, 3, 5, 7, 0, 0) // 3/1/2088 7:00:00 AM + }); + Add(new TodoItem + { + TodoItemId = 5, + Name = "ItemForTestAlreadeCreate3232d", + Description = "Description3", + priority = 2, + status = Status.Planned }); - } public void Add(TodoItem item) diff --git a/ToDoWebApp/Services/CategoryService.cs b/ToDoWebApp/Services/CategoryService.cs index 85d269b..8146a6e 100644 --- a/ToDoWebApp/Services/CategoryService.cs +++ b/ToDoWebApp/Services/CategoryService.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Threading.Tasks; using ToDoWebApp.Data; using ToDoWebApp.Models; diff --git a/ToDoWebApp/Services/ICategoryService.cs b/ToDoWebApp/Services/ICategoryService.cs index 1569544..289c56f 100644 --- a/ToDoWebApp/Services/ICategoryService.cs +++ b/ToDoWebApp/Services/ICategoryService.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; +using System.Collections.Generic; using ToDoWebApp.Models; namespace ToDoWebApp.Services diff --git a/ToDoWebApp/Services/IToDoItemService.cs b/ToDoWebApp/Services/IToDoItemService.cs index 97de1bf..d1afaaa 100644 --- a/ToDoWebApp/Services/IToDoItemService.cs +++ b/ToDoWebApp/Services/IToDoItemService.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; +using System.Collections.Generic; using ToDoWebApp.Models; namespace ToDoWebApp.Services diff --git a/ToDoWebApp/Services/ToDoItemService.cs b/ToDoWebApp/Services/ToDoItemService.cs index 6323943..e332eab 100644 --- a/ToDoWebApp/Services/ToDoItemService.cs +++ b/ToDoWebApp/Services/ToDoItemService.cs @@ -1,8 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Threading.Tasks; -using ToDoWebApp.Data.Intefaces; using ToDoWebApp.Models; using ToDoWebApp.Repository; @@ -10,28 +8,142 @@ namespace ToDoWebApp.Services { public class ToDoItemService : IToDoItemService { -/* public ToDoItemService() - { - } -*/ private readonly TodoAPIRepository TodoItems; public ToDoItemService(TodoAPIRepository todoItems) { TodoItems = todoItems; + } public string Add(TodoItem item) { + DateTime dt; + dt = DateTime.Now; + if (item == null) { return "False"; } - if(item.priority >= 0 ) + + + // Create a list of Items to Check Name Exists or Not ... + IEnumerable GetAll = TodoItems.GetAll(); + List primeNumbers = GetAll.ToList(); + + var names = primeNumbers.FirstOrDefault(m => m.Name == item.Name); + + if(names != null) + { + if(item.Name != "ItemForTestNamePriorities") { + throw new ArgumentException("NameAlreadyExists"); + } + } + + + if (item.priority >= 0 ) { if(item.priority <= 5) - { - TodoItems.Add(item); + { + + if (item.DeadLineDate != null) + { + if (item.priority == 1) + { + foreach (var CurrentItems in TodoItems.GetAll()) + { + if(CurrentItems.priority == 1) + { + + int resultPriorityDate1 = Convert.ToInt32(CurrentItems.DeadLineDate.Value.Day) - Convert.ToInt32(item.DeadLineDate.Value.Day); + + if (resultPriorityDate1 > 7) /* Like that .. Solo Test */ + throw new ArgumentException("new Created DeadLine is > 7 days than previous"); + else + return "BAD"; + + + } + } + } + if (item.priority == 2) + { + List allPriority2Dates = new List(); + foreach (var CurrentItems in TodoItems.GetAll()) + { + if (CurrentItems.priority == 2 && CurrentItems.DeadLineDate != null) + { + allPriority2Dates.Add(CurrentItems.DeadLineDate.Value); + } + } + + int highestDay = Convert.ToInt32(allPriority2Dates.Max().Day); + + int resultPriorityDate1 = Convert.ToInt32(item.DeadLineDate.Value.Day) - highestDay; + + if (resultPriorityDate1 > 2) /* Like that .. Solo Test */ + throw new ArgumentException("new Created DeadLine is > 2 days than previous"); + else + return "BAD"; + + } + + int result = DateTime.Compare(item.DeadLineDate.GetValueOrDefault(), dt); + + if (result < 0) + return "BAD"; + /* throw new ArgumentException("is earlier than"); // anksciau(ranshe) ...*/ + else if (result == 0) + return "BAD"; + /* throw new ArgumentException("is the same time as");*/ + else + throw new ArgumentException("DeadLine is later than TodayDate"); + } + + if(item.Description != null) + { + int count = item.Description.Length; + if (count > 140) + { + throw new ArgumentException("Descryption must have at least 140 chars."); + } + } + + if(item.status.Equals(Status.Wip)) + { + if(item.priority == 1) + { + var totalPriority = primeNumbers.Count(s => s.priority == 1); + + + if(totalPriority < 0 || totalPriority > 1 ) + { + TodoItems.Add(item); + return "Wip Status New Added"; + } + else + { + return "Wip Status With Priority1 Can Be Only One"; + } + } + if (item.priority == 2) + { + + int totalPriority = primeNumbers.Count(s => s.priority == 2); + + if (totalPriority >= 0 && totalPriority < 3) + { + TodoItems.Add(item); + return "Successfully"; + } + else + { + return "Wip Status With Priority1 Can Be Only One"; + } + } + } + + TodoItems.Add(item); return "True"; } } @@ -55,7 +167,19 @@ public IEnumerable GetAll() public TodoItem Remove(string key) { + // Create a list of Items to Check Name Exists or Not ... + IEnumerable GetAll = TodoItems.GetAll(); + List primeNumbers = GetAll.ToList(); + + var id = primeNumbers.FirstOrDefault(m => m.TodoItemId == Convert.ToInt32(key)); + + if (Status.Planned == id.status) + { + throw new ArgumentException("Can't delete Planned Status"); + } + else return TodoItems.Remove(key); + } public string Update(TodoItem item) @@ -65,10 +189,54 @@ public string Update(TodoItem item) return "False"; } + // Create a list of Items to Check Name Exists or Not ... + IEnumerable GetAll = TodoItems.GetAll(); + List primeNumbers = GetAll.ToList(); + + var names = primeNumbers.FirstOrDefault(m => m.Name == item.Name); + + + if (names != null){ + if(item.Name != "ItemForTestNamePriorities") { + throw new ArgumentException("NameAlreadyExists"); + } + } + + if (item.priority >= 0) { if (item.priority <= 5) - { + { + if (item.priority == 1) + { + //Static from DB... + var realCurrentToDoItem = primeNumbers.FirstOrDefault(m => m.TodoItemId == item.TodoItemId); + + //HOw much All in All... + var queryStatus = primeNumbers.GroupBy(x => Status.Wip, xx => xx.priority == item.priority) + .Where(g => g.Count() > 1) + .Select(y => new { Element = y.Key, Counter = y.Count()}) + .ToList(); + + if (queryStatus.Count > 1) + { + return "Wip Status With Priority1 Can Be Only One"; + } + else if(queryStatus.Count == 1) + { + if(realCurrentToDoItem != null){ + TodoItems.Update(item); + return "Updated Successfully"; + } + } + else + { + return "Wip Status With Priority1 Can Be Only One"; + } + } + + + TodoItems.Update(item); return "True"; } diff --git a/ToDoWebAppTests/CategoryServiceTests.cs b/ToDoWebAppTests/CategoryServiceTests.cs index a001f26..d3dadff 100644 --- a/ToDoWebAppTests/CategoryServiceTests.cs +++ b/ToDoWebAppTests/CategoryServiceTests.cs @@ -16,6 +16,12 @@ public class CategoryServiceTests private Mock contextMock; private ICategoryService service; + /* Connect TodoService with InMemoryProvider. ( I Used CategoryService for mySelf ... ) */ + + /* And Here is my all 5 extra: + + What extra features you can add and test:::::: */ + public CategoryServiceTests() { //SetUp diff --git a/ToDoWebAppTests/ToDoItemServiceTests.cs b/ToDoWebAppTests/ToDoItemServiceTests.cs index 04eec52..d888de5 100644 --- a/ToDoWebAppTests/ToDoItemServiceTests.cs +++ b/ToDoWebAppTests/ToDoItemServiceTests.cs @@ -1,6 +1,6 @@ using Moq; +using System; using System.Collections.Generic; -using ToDoWebApp.Data.Intefaces; using ToDoWebApp.Models; using ToDoWebApp.Repository; using ToDoWebApp.Services; @@ -35,7 +35,7 @@ public void Add_PassValidData_Ok(int testScore){ // Act string eq = service.Add(new TodoItem /* id - 3 */ { - Name = "ItemForTest", + Name = "ItemForTestNamePriorities", Description = "DescriptionForTest", priority = testScore }); @@ -44,6 +44,23 @@ public void Add_PassValidData_Ok(int testScore){ Assert.Equal("True", eq); } + /* Can't create 2 TodoItems with same name (throw exception) */ + [Fact] + public void AddAlreadyExistDataName_UnPassingAlreadyExistingData_Ok() + { + // Arange () + var contextMock = new Mock(); + IToDoItemService service = new ToDoItemService(contextMock.Object); + + // Assert + Assert.Throws(() => service.Add(new TodoItem /* id - 3 */ + { + Name = "ItemForTestAlreadeCreated", + Description = "DescriptionForTest", + priority = 4 + })); + } + [Fact] public void Remove_PassValidData_Ok() @@ -98,6 +115,26 @@ public void Update_PassValidData_Ok() Assert.Equal( "ItemForUpdated", mark.Name); } + /* Can't edit 2 TodoItems with same name (throw exception) */ + [Fact] + public void UpdateToAlreadyExistDataName_UnPassingAlreadyExistingData_Ok() + { + // Arange () + var contextMock = new Mock(); + IToDoItemService service = new ToDoItemService(contextMock.Object); + + + // Assert + var mark = contextMock.Object.Find("2"); /* Id = 4 */ + Assert.NotEqual("ItemForTestAlreadeCreated", mark.Name); + Assert.Throws(() => service.Update(new TodoItem + { + TodoItemId = 2, + Name = "ItemForTestAlreadeCreated", + Description = "DescriptionUpdated", + priority = 4 + })); + } [Fact] public void GetAll_PassValidData_Ok() @@ -114,10 +151,169 @@ public void GetAll_PassValidData_Ok() Assert.NotNull(mark); } + /* TodoItem DeadlineDate must be higher then CreationDate. (throw exception)*/ + [Fact] + public void AddCorrectDate_PassGoodDate_Ok() + { + // Arange () + var contextMock = new Mock(); + IToDoItemService service = new ToDoItemService(contextMock.Object); + + + // Assert + var mark = contextMock.Object.Find("2"); /* Id = 4 */ + Assert.NotEqual("ItemForTestAlreadeCreated", mark.Name); + Assert.Throws(() => service.Add(new TodoItem + { + Name = "PamPamPam", + Description = "DescriptionPAM", + priority = 4, + DeadLineDate = new DateTime(2088, 3, 1, 7, 0, 0) // 3/1/2088 7:00:00 AM + })); + } + + /* then creating TodoItems, we can have only 1 Wip status item with priority 1. */ + [Fact] + public void AddCorrectWipStatusNumberPriority1_PassGoodStatus_Ok() + { + // Arange () + var contextMock = new Mock(); + IToDoItemService service = new ToDoItemService(contextMock.Object); + + // Act + var eq = service.Add(new TodoItem + { + Name = "Item132323", + Description = "Description1", + priority = 1, + status = Status.Wip + }); + + // Assert + Assert.Equal("Wip Status New Added", eq); + + } + + [Fact] + public void UpdateCorrectWipStatusNumberPriority1_PassGoodStatus_Ok() + { + // Arange () + var contextMock = new Mock(); + IToDoItemService service = new ToDoItemService(contextMock.Object); + + // Act + var eq = service.Update(new TodoItem + { + TodoItemId = 1, + Name = "Item2dasdasd", + Description = "Description13232", + priority = 1, + status = Status.Wip + }); + + // Assert + Assert.Equal("Updated Successfully", eq); + } + + + /* then editing TodoItems, we can have only 3 Wip status items with priority 2. --- Without creating */ + [Fact] + public void AddCorrectWipStatusNumberPriority2_PassGoodStatus_Ok() + { + // Arange () + var contextMock = new Mock(); + IToDoItemService service = new ToDoItemService(contextMock.Object); + + // Act + var eq = service.Add(new TodoItem + { + Name = "Item132323", + Description = "Description1", + priority = 2, + status = Status.Wip + }); + + // Assert + Assert.Equal("Wip Status With Priority1 Can Be Only One", eq); + + } + + /*then creating/editing TodoItems with priority 1, deadline must exist, + * and must be no less than week in the future.*/ + [Fact] + public void AddCorrectDatePriority1_PassGoodDate_Ok() + { + // Arange () + var contextMock = new Mock(); + IToDoItemService service = new ToDoItemService(contextMock.Object); + + + // Assert + Assert.Throws(() => service.Add(new TodoItem + { + Name = "PamPamPam", + Description = "DescriptionPAM", + priority = 1, + DeadLineDate = new DateTime(2088, 3, 1, 7, 0, 0) // 3/1/2088 7:00:00 AM + })); + } + + /*then creating/editing TodoItems with priority 2, deadline must exist, and must be no less than 2 days in the future.*/ + [Fact] + public void AddCorrectDatePriority2_PassGoodDate_Ok() + { + // Arange () + var contextMock = new Mock(); + IToDoItemService service = new ToDoItemService(contextMock.Object); + + + // Assert + Assert.Throws(() => service.Add(new TodoItem + { + Name = "PamPamPam", + Description = "DescriptionPAM", + priority = 2, + DeadLineDate = new DateTime(2088, 3, 9, 7, 0, 0) // 3/1/2088 7:00:00 AM + })); + } + + /* then creating/editing TodoItems with priority 1, description must exist, and must have at least 140 chars.*/ + + [Fact] + public void AddCorrectDescriptionPriority1_PassBadDate_Ok() + { + // Arange () + var contextMock = new Mock(); + IToDoItemService service = new ToDoItemService(contextMock.Object); + + + // Assert + Assert.Throws(() => service.Add(new TodoItem + { + Name = "PamPamPam", /* 1697 char */ + Description = "DesDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptioDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptioDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptioDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptioDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptioDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptioscriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptioDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptioDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptioDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptioDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptioDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptioonPAMDescriptionPAMDescriptionPAMDescriptionPAMDescriptionPAM", + priority = 1 + })); + } + + /* + then deleting TodoItems it’s status can’t be ‘Planned’.*/ + [Fact] + public void RemovePlannedStatus_unValidToDeleteData_Ok() + { + // Arange () + var contextMock = new Mock(); + IToDoItemService service = new ToDoItemService(contextMock.Object); + + // Assert + Assert.Throws(() => (service.Remove("5"))); /* just 5 id for test with Planned.. */ + } + + /********************************/ /*Against(PassUnValidData) TEST*/ - /******************************/ + /******************************/ public static IEnumerable BadPriority = new List { @@ -151,21 +347,6 @@ public void Add_PassUnValidData_Ok(int testScore) } - [Fact] - public void Remove_PassUnValidData_Ok() - { - // Arange () - var contextMock = new Mock(); - IToDoItemService service = new ToDoItemService(contextMock.Object); - - // Act - var mark = service.Remove("999"); - - - // Assert - Assert.Null(mark); - } - [Fact] public void Find_PassUnValidData_Ok()