Skip to content

Commit

Permalink
Feature/#67 (#68)
Browse files Browse the repository at this point in the history
* WatchList.WinForms - Added input parameter "IMessageBox" to form constructors.

* WatchList.Core - Changed constructor class "DownloadDataService" and the place to call the extension method "ReplaceIdIsNotFree".

* Solution - Changes using "WatchItemRepository" instead of "WatchCinemaDbContext".

* WatchList.WinForms - Add DI container in "Program".

* Solution - Add interface "ILoadRulesConfig" and class "TestLoadRuleConfig"; Rename "ModelProcessUploadData" -> "LoadRulesConfigModel"; Used new class and interface.

* WatchList.Test - Add folder "Components" and transferred the classes "TestAppDbContextFactory" and "TestLoadRuleConfig"

* WatchList.Test - Add class "TestAggregateLoadRule" and used it in "Download..." test classes.
  • Loading branch information
Stan-Kudri authored Sep 6, 2023
1 parent 7ab1d73 commit 97a8f97
Show file tree
Hide file tree
Showing 32 changed files with 265 additions and 164 deletions.
15 changes: 15 additions & 0 deletions WatchList.Core/Model/Load/ILoadRulesConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using WatchList.Core.Model.ItemCinema.Components;

namespace WatchList.Core.Model.Load
{
public interface ILoadRulesConfig
{
bool DeleteGrade { get; }

ActionDuplicateItems ActionsWithDuplicates { get; }

TypeCinema TypeCinemaLoad { get; }

Grade MoreGrade { get; }
}
}
14 changes: 14 additions & 0 deletions WatchList.Core/Repository/Extension/DuplicateIdExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using WatchList.Core.Model.ItemCinema;
using WatchList.Core.Repository.Db;

namespace WatchList.Core.Repository.Extension
{
public static class DuplicateIdExtension
{
public static Guid ReplaceIdIsNotFree(this WatchCinemaDbContext dbContext, WatchItem item)
{
var idDuplicate = dbContext.WatchItem.Where(x => x.Id == item.Id).Take(2).Select(x => x.Id).ToList();
return idDuplicate.Count != 0 ? Guid.NewGuid() : item.Id;
}
}
}
11 changes: 11 additions & 0 deletions WatchList.Core/Repository/WatchItemRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public PagedList<WatchItem> GetPage(WatchItemSearchRequest searchRequest)

public void Add(WatchItem item)
{
item.Id = _db.ReplaceIdIsNotFree(item);
_db.Add(item);
_db.SaveChanges();
}
Expand Down Expand Up @@ -52,5 +53,15 @@ public void Update(WatchItem editItem)

_db.SaveChanges();
}

public List<Guid> SelectDuplicateItems(WatchItem item) =>
_db.WatchItem.Where(x =>
(x.Title == item.Title && x.Sequel == item.Sequel && x.Type == item.Type)).
Take(2).Select(x => x.Id).ToList();

public List<Guid> DuplicateItemsCaseSensitive(WatchItem item) =>
_db.WatchItem.ToList().
Where(x => x.TitleNormalized == item.TitleNormalized && x.Sequel == item.Sequel && x.Type == item.Type).
Take(2).Select(x => x.Id).ToList();
}
}
14 changes: 4 additions & 10 deletions WatchList.Core/Service/DataLoading/DownloadDataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,25 @@
using WatchList.Core.Model.Sorting;
using WatchList.Core.PageItem;
using WatchList.Core.Repository;
using WatchList.Core.Repository.Db;
using WatchList.Core.Service.Component;
using WatchList.Core.Service.DataLoading.Rules;
using WatchList.Core.Service.Extension;

namespace WatchList.Core.Service.DataLoading
{
public class DownloadDataService
{
private readonly WatchCinemaDbContext _db;

private readonly WatchItemRepository _repository;

private readonly IMessageBox _messageBox;

public DownloadDataService(WatchCinemaDbContext db, IMessageBox messageBox)
public DownloadDataService(WatchItemRepository repository, IMessageBox messageBox, int numberOfItemPerPage = 500)
{
_db = db;
db.ChangeTracker.Clear();
_repository = new WatchItemRepository(_db);
_repository = repository;
_messageBox = messageBox;
NumberOfItemPerPage = numberOfItemPerPage;
}

public int NumberOfItemPerPage { get; set; } = 500;
public int NumberOfItemPerPage { get; set; }

public void Download(WatchItemRepository repository, ILoadRule loadRule)
{
Expand Down Expand Up @@ -55,7 +50,6 @@ private void AddItems(WatchItemCollection itemCollection)

foreach (var item in itemCollection.NewItems)
{
item.Id = _db.ReplaceIdIsNotFree(item);
_repository.Add(item);
}
}
Expand Down
3 changes: 2 additions & 1 deletion WatchList.Core/Service/DataLoading/Rules/DeleteGradeRule.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using WatchList.Core.Model.ItemCinema.Components;
using WatchList.Core.Model.Load;

namespace WatchList.Core.Service.DataLoading.Rules
{
public class DeleteGradeRule : ILoadRule
{
public DeleteGradeRule(bool isDeleteGrade) => IsDeleteGrade = isDeleteGrade;
public DeleteGradeRule(ILoadRulesConfig config) => IsDeleteGrade = config.DeleteGrade;

public bool IsDeleteGrade { get; private set; }

Expand Down
13 changes: 7 additions & 6 deletions WatchList.Core/Service/DataLoading/Rules/DuplicateLoadRule.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using WatchList.Core.Model.Load;
using WatchList.Core.Repository.Db;
using WatchList.Core.Service.Extension;
using WatchList.Core.Repository;

namespace WatchList.Core.Service.DataLoading.Rules
{
Expand All @@ -12,11 +11,13 @@ public class DuplicateLoadRule : ILoadRule

private readonly bool _caseSensitive;

private readonly WatchCinemaDbContext _dbContext;
private readonly WatchItemRepository _itemRepository;

public DuplicateLoadRule(WatchCinemaDbContext db, ActionDuplicateItems actionsWithDuplicates)
public DuplicateLoadRule(WatchItemRepository itemRepository, ILoadRulesConfig config)
{
_dbContext = db;
var actionsWithDuplicates = config.ActionsWithDuplicates;

_itemRepository = itemRepository;
_actionSelected = actionsWithDuplicates.ActionSelected;
if (_actionSelected)
{
Expand All @@ -38,7 +39,7 @@ public WatchItemCollection Apply(WatchItemCollection items)

foreach (var item in items.Items)
{
var selectItem = _caseSensitive && _actionSelected ? _dbContext.WatchItem.SelectDuplicateItems(item) : _dbContext.WatchItem.DuplicateItemsCaseSensitive(item);
var selectItem = _caseSensitive && _actionSelected ? _itemRepository.SelectDuplicateItems(item) : _itemRepository.DuplicateItemsCaseSensitive(item);
if (selectItem.Count == 0)
{
idAddItems.Add(item.Id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ namespace WatchList.Core.Service.DataLoading.Rules
{
public class FilterByMoreGradeLoadRule : ILoadRule
{
public FilterByMoreGradeLoadRule(Grade moreGrade)
{
MoreGrade = moreGrade;
}
public FilterByMoreGradeLoadRule(ILoadRulesConfig config) => MoreGrade = config.MoreGrade;

public Grade MoreGrade { get; private set; }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using WatchList.Core.Model.ItemCinema.Components;
using WatchList.Core.Model.Load;

namespace WatchList.Core.Service.DataLoading.Rules
{
public class FilterByTypeCinemaLoadRule : ILoadRule
{
public FilterByTypeCinemaLoadRule(TypeCinema typeCinema) => TypeCinemaDataLoad = typeCinema;
public FilterByTypeCinemaLoadRule(ILoadRulesConfig config) => TypeCinemaDataLoad = config.TypeCinemaLoad;

public TypeCinema TypeCinemaDataLoad { get; private set; }

Expand Down
25 changes: 0 additions & 25 deletions WatchList.Core/Service/Extension/DuplicateItemExtension.cs

This file was deleted.

15 changes: 5 additions & 10 deletions WatchList.Core/Service/WatchItemService.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
using WatchList.Core.Model.ItemCinema;
using WatchList.Core.PageItem;
using WatchList.Core.Repository;
using WatchList.Core.Repository.Db;
using WatchList.Core.Service.Component;
using WatchList.Core.Service.Extension;

namespace WatchList.Core.Service
{
public class WatchItemService
{
public const string DuplicateReplaceMessage = "The append item is a duplicate. Replace element?";

private readonly WatchCinemaDbContext _db;

private readonly WatchItemRepository _repository;

private readonly IMessageBox _messageBox;

public WatchItemService(WatchCinemaDbContext dbContext, IMessageBox messageBox)
public WatchItemService(WatchItemRepository itemRepository, IMessageBox messageBox)
{
_db = dbContext;
_repository = new WatchItemRepository(_db);
_repository = itemRepository;
_messageBox = messageBox;
}

Expand All @@ -30,7 +25,7 @@ public WatchItemService(WatchCinemaDbContext dbContext, IMessageBox messageBox)

public void Add(WatchItem item)
{
var selectItem = _db.WatchItem.SelectDuplicateItems(item);
var selectItem = _repository.SelectDuplicateItems(item);
var countDuplicate = selectItem.Count;

if (countDuplicate == 0)
Expand All @@ -48,10 +43,10 @@ public void Add(WatchItem item)

public void Update(WatchItem oldItem, WatchItem modifiedItem)
{
var selectItem = _db.WatchItem.SelectDuplicateItems(modifiedItem);
var selectItem = _repository.SelectDuplicateItems(modifiedItem);
var countDuplicate = selectItem.Count;

if (countDuplicate == 1)
if (countDuplicate == 1 && oldItem.Title != modifiedItem.Title)
{
if (_messageBox.ShowQuestionSaveItem(DuplicateReplaceMessage))
{
Expand Down
19 changes: 19 additions & 0 deletions WatchList.Test/Components/TestAggregateLoadRule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using WatchList.Core.Repository;
using WatchList.Core.Service.DataLoading.Rules;

namespace WatchList.Test.Components
{
public class TestAggregateLoadRule : AggregateLoadRule
{
public TestAggregateLoadRule(WatchItemRepository itemRepository, TestLoadRuleConfig loadRulesConfig)
: base(new ILoadRule[]
{
new DeleteGradeRule(loadRulesConfig),
new FilterByTypeCinemaLoadRule(loadRulesConfig),
new FilterByMoreGradeLoadRule(loadRulesConfig),
new DuplicateLoadRule(itemRepository, loadRulesConfig),
})
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Microsoft.EntityFrameworkCore;
using WatchList.Core.Repository.Db;

namespace WatchList.Test
namespace WatchList.Test.Components
{
public class TestAppDbContextFactory
{
Expand Down
28 changes: 28 additions & 0 deletions WatchList.Test/Components/TestLoadRuleConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using WatchList.Core.Model.ItemCinema.Components;
using WatchList.Core.Model.Load;

namespace WatchList.Test.Components
{
public class TestLoadRuleConfig : ILoadRulesConfig
{
public TestLoadRuleConfig()
{
}

public TestLoadRuleConfig(bool deleteGrade, TypeCinema typeCinema, Grade grade, ActionDuplicateItems actionDuplicateItems)
{
DeleteGrade = deleteGrade;
TypeCinemaLoad = typeCinema;
MoreGrade = grade;
ActionsWithDuplicates = actionDuplicateItems;
}

public bool DeleteGrade { get; set; } = false;

public TypeCinema TypeCinemaLoad { get; set; } = TypeCinema.AllType;

public Grade MoreGrade { get; set; } = Grade.AnyGrade;

public ActionDuplicateItems ActionsWithDuplicates { get; set; } = new ActionDuplicateItems();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using WatchList.Core.Model.ItemCinema.Components;
using WatchList.Core.Model.Sorting;
using WatchList.Core.PageItem;
using WatchList.Test.Components;

namespace WatchList.Test.CoreTest.FilterItemTest
{
Expand Down
1 change: 1 addition & 0 deletions WatchList.Test/CoreTest/FilterItemTest/FilterByTypeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using WatchList.Core.Model.ItemCinema.Components;
using WatchList.Core.Model.Sorting;
using WatchList.Core.PageItem;
using WatchList.Test.Components;

namespace WatchList.Test.CoreTest.FilterItemTest
{
Expand Down
1 change: 1 addition & 0 deletions WatchList.Test/CoreTest/SortFieldTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using WatchList.Core.Model.ItemCinema.Components;
using WatchList.Core.Model.Sorting;
using WatchList.Core.PageItem;
using WatchList.Test.Components;

namespace WatchList.Test.CoreTest
{
Expand Down
1 change: 1 addition & 0 deletions WatchList.Test/CoreTest/WatchItemRepositoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using WatchList.Core.Model.Sorting;
using WatchList.Core.PageItem;
using WatchList.Core.Repository;
using WatchList.Test.Components;

namespace WatchList.Test.CoreTest
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
using Moq;
using WatchList.Core.Model.ItemCinema;
using WatchList.Core.Model.ItemCinema.Components;
using WatchList.Core.Repository;
using WatchList.Core.Service;
using WatchList.Core.Service.Component;
using WatchList.Test.Components;

namespace WatchList.Test.CoreTest.WatchItemServiceTest
{
Expand Down Expand Up @@ -56,9 +58,10 @@ public void Add_With_Replace_Duplicate_Element(List<WatchItem> items, WatchItem
{
// Arrange
var dbContext = new TestAppDbContextFactory().Create();
var itemRepository = new WatchItemRepository(dbContext);
var messageBox = new Mock<IMessageBox>();
messageBox.Setup(foo => foo.ShowQuestionSaveItem(WatchItemService.DuplicateReplaceMessage)).Returns(true);
var service = new WatchItemService(dbContext, messageBox.Object);
var service = new WatchItemService(itemRepository, messageBox.Object);
dbContext.AddRange(items);
dbContext.SaveChanges();
service.Add(addItem);
Expand Down
Loading

0 comments on commit 97a8f97

Please sign in to comment.