-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(application): adding concretes for query and file commands concr…
…etes
- Loading branch information
1 parent
c8683df
commit 4b990c8
Showing
3 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
...Application/Tables/Components/QueryCommands/Concretes/File/Concretes/CreateJsonCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
namespace DistributionCenter.Application.Tables.Components.QueryCommands.Concretes.File.Concretes; | ||
|
||
using Bases; | ||
using DistributionCenter.Application.Tables.Connections.Interfaces; | ||
using DistributionCenter.Commons.Results; | ||
using DistributionCenter.Domain.Entities.Interfaces; | ||
|
||
public class CreateJsonCommand<T>( | ||
IFileConnectionFactory<T> fileConnectionFactory, | ||
T entity | ||
) : BaseJsonCommand<T>(fileConnectionFactory, entity) | ||
where T : IEntity | ||
{ | ||
private readonly T _entity = entity; | ||
private readonly IFileConnectionFactory<T> _fileConnectionFactory = fileConnectionFactory; | ||
protected override async Task<Result> Execute(IEnumerable<T> data) | ||
{ | ||
IEnumerable<T> entityIntoList = [_entity]; | ||
IEnumerable<T> updatedData = data.Concat(entityIntoList); | ||
|
||
await _fileConnectionFactory.SaveDataAsync(updatedData); | ||
return Result.Ok(); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
....Application/Tables/Components/QueryCommands/Concretes/File/Concretes/GetByIdJsonQuery.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
namespace DistributionCenter.Application.Tables.Components.QueryCommands.Concretes.File.Concretes; | ||
|
||
using Bases; | ||
using Commons.Errors; | ||
using Commons.Results; | ||
using Connections.Interfaces; | ||
using Domain.Entities.Interfaces; | ||
|
||
public class GetByIdJsonQuery<T>(IFileConnectionFactory<T> fileConnectionFactory, Guid id, T entity) | ||
: BaseJsonQuery<T>(fileConnectionFactory, entity) | ||
where T : IEntity | ||
{ | ||
private Guid Id { get; } = id; | ||
|
||
protected override Task<Result<T>> Execute(IEnumerable<T> data) | ||
{ | ||
T? entity = data.FirstOrDefault(p => p.Id == Id); | ||
|
||
if (entity is null) | ||
{ | ||
return Task.FromResult<Result<T>>(Error.NotFound( | ||
code: "ENTITY_NOT_FOUND", | ||
description: $"The {typeof(T).Name} was not found.")); | ||
} | ||
|
||
return Task.FromResult<Result<T>>(entity); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...Application/Tables/Components/QueryCommands/Concretes/File/Concretes/UpdateJsonCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
namespace DistributionCenter.Application.Tables.Components.QueryCommands.Concretes.File.Concretes; | ||
|
||
using Bases; | ||
using Commons.Errors; | ||
using Commons.Results; | ||
using Connections.Interfaces; | ||
using Domain.Entities.Interfaces; | ||
|
||
public class UpdateJsonCommand<T>(IFileConnectionFactory<T> fileConnectionFactory, | ||
T entity | ||
) : BaseJsonCommand<T>(fileConnectionFactory, entity) | ||
where T : IEntity | ||
{ | ||
private readonly T _entity = entity; | ||
private readonly IFileConnectionFactory<T> _fileConnectionFactory = fileConnectionFactory; | ||
|
||
protected override async Task<Result> Execute(IEnumerable<T> data) | ||
{ | ||
T? found = data.FirstOrDefault(p => p.Id == _entity.Id); | ||
|
||
if (found == null) | ||
{ | ||
return Error.NotFound( | ||
code: "ENTITY_NOT_FOUND", | ||
description: $"The {typeof(T).Name} with ID {_entity.Id} was not found." | ||
); | ||
} | ||
|
||
IEnumerable<T> updatedData = data.Select(p => p.Id == _entity.Id ? _entity : p); | ||
await _fileConnectionFactory.SaveDataAsync(updatedData); | ||
return Result.Ok(); | ||
} | ||
} |