Skip to content

Commit

Permalink
feat(application): adding concretes for query and file commands concr…
Browse files Browse the repository at this point in the history
…etes
  • Loading branch information
JeferssonCL committed Sep 12, 2024
1 parent c8683df commit 4b990c8
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
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();
}
}
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);
}
}
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();
}
}

0 comments on commit 4b990c8

Please sign in to comment.