Skip to content

TDD homework (With Extra Own Tests) #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: HomeWork
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions ToDoWebApp/Data/Models/TodoItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public string Name
}
/**/
[DataType(DataType.Text)]
[StringLength(140)]
public string? Description
{
get;
Expand Down Expand Up @@ -94,7 +95,8 @@ public enum Status
[Display(Name = "Done")]
Done,
[Display(Name = "Archived")]
Archived

Archived,
[Display(Name = "Planned")]
Planned
}
}
39 changes: 35 additions & 4 deletions ToDoWebApp/Repository/TodoAPIRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,51 @@ public class TodoAPIRepository : ITodoItemAPIRepository
public List<TodoItem> 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)
Expand Down
1 change: 0 additions & 1 deletion ToDoWebApp/Services/CategoryService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ToDoWebApp.Data;
using ToDoWebApp.Models;

Expand Down
5 changes: 1 addition & 4 deletions ToDoWebApp/Services/ICategoryService.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 1 addition & 4 deletions ToDoWebApp/Services/IToDoItemService.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
188 changes: 178 additions & 10 deletions ToDoWebApp/Services/ToDoItemService.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,149 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ToDoWebApp.Data.Intefaces;
using ToDoWebApp.Models;
using ToDoWebApp.Repository;

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<TodoItem> GetAll = TodoItems.GetAll();
List<TodoItem> 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<DateTime> allPriority2Dates = new List<DateTime>();
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";
}
}
Expand All @@ -55,7 +167,19 @@ public IEnumerable<TodoItem> GetAll()

public TodoItem Remove(string key)
{
// Create a list of Items to Check Name Exists or Not ...
IEnumerable<TodoItem> GetAll = TodoItems.GetAll();
List<TodoItem> 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)
Expand All @@ -65,10 +189,54 @@ public string Update(TodoItem item)
return "False";
}

// Create a list of Items to Check Name Exists or Not ...
IEnumerable<TodoItem> GetAll = TodoItems.GetAll();
List<TodoItem> 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";
}
Expand Down
6 changes: 6 additions & 0 deletions ToDoWebAppTests/CategoryServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ public class CategoryServiceTests
private Mock<ToDoContext> 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
Expand Down
Loading