Skip to content

Commit

Permalink
Refactor handlers, update DTOs, and add service configs
Browse files Browse the repository at this point in the history
Refactored command handlers to use DataMapper's Map method.
Updated AddDishCommand to include RestaurantId.
Refactored DishesController to use IDishesRepository.
Updated DataMapper with new Map methods.
Enhanced ResturantDTO to include a list of Dish objects.
Commented out middleware in Program.cs.
Modified DishesRepository to return dish ID.
Updated IDishesRepository method signatures.
Added UserSecretsId to Resturants.csproj.
Introduced AddDishDTO and DishDTO classes.
Added serviceDependencies.json and serviceDependencies.local.json.
  • Loading branch information
nahiandev committed Jan 2, 2025
1 parent c6dc610 commit 20026bf
Show file tree
Hide file tree
Showing 19 changed files with 117 additions and 91 deletions.
3 changes: 1 addition & 2 deletions src/Actions/Commands/AddDish/AddDishCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ namespace Resturants.Actions.Commands.AddDish
{
public class AddDishCommand : IRequest
{
public int Id { get; set; }
public string Name { get; set; } = default!;
public string Description { get; set; } = default!;
public decimal Price { get; set; }

public int? KiloCalories { get; set; }

// public int RestaurantId { get; set; }
public int RestaurantId { get; set; }
}
}
25 changes: 12 additions & 13 deletions src/Actions/Commands/AddDish/AddDishCommandHandler.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
using MediatR;
using Restaurants.DomainMapper;
using Restaurants.Domains.Models;
using Restaurants.Repository.Interfaces;
using Resturants.Repository.Interfaces;

namespace Resturants.Actions.Commands.AddDish
{
public class AddDishCommandHandler : IRequestHandler<AddDishCommand>
{
private readonly IDishesRepository _repository;

private readonly IDishesRepository _dish_repository;
private readonly IResturantRepository _resturant_repository;
private readonly ILogger<AddDishCommandHandler> _logger;

public AddDishCommandHandler(IDishesRepository repository, ILogger<AddDishCommandHandler> logger)
public AddDishCommandHandler(IDishesRepository dish_repository,
IResturantRepository resturant_repository,
ILogger<AddDishCommandHandler> logger)
{
_repository = repository;

_dish_repository = dish_repository;
_resturant_repository = resturant_repository;
_logger = logger;
}
public async Task Handle(AddDishCommand request, CancellationToken cancellation_token)

Check warning on line 23 in src/Actions/Commands/AddDish/AddDishCommandHandler.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
_logger.LogInformation("Adding new dish {@DishRequest}", request);

Dish domain_dish = new()
{
Name = request.Name,
Price = request.Price,
Description = request.Description
};
//var id = request.RestaurantId;

await _repository.AddDishAsync(domain_dish, cancellation_token);
//var resturant = await _resturant_repository.GetResturantByIdAsync(id) ?? throw new Exception(nameof(Resturant));

throw new NotImplementedException();

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public AddResturantCommandHandler(IResturantRepository repository)
}
public async Task<int> Handle(AddResturantCommand request, CancellationToken cancellationToken)
{
var domain_resturant = DataMapper.Instance.Mapper(request);
var domain_resturant = DataMapper.Instance.Map(request);

var saved_resturant = await _repository.AddResturantAsync(domain_resturant!);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public async Task<bool> Handle(UpdateResturantCommand request, CancellationToken
{
var id = request.Id;

var domain_resturant = DataMapper.Instance.Mapper(request);
var domain_resturant = DataMapper.Instance.Map(request);

var is_updated = await _repository.UpdateResturantAsync(id, domain_resturant);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public GetResturantByIdQueryHandler(IResturantRepository repository)

if (resturant is null) return await Task.FromResult<ResturantDTO?>(null);

return DataMapper.Instance.Mapper(resturant);
return DataMapper.Instance.Map(resturant);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public async Task<IEnumerable<ResturantDTO>> Handle(GetResturantsQuery request,

if (resturants.Count is 0) return [];

return [.. resturants.Select(DataMapper.Instance.Mapper)];
return [.. resturants.Select(DataMapper.Instance.Map)];
}
}
}
51 changes: 16 additions & 35 deletions src/Controllers/DishesController.cs
Original file line number Diff line number Diff line change
@@ -1,59 +1,40 @@
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Metadata;
using Restaurants.DomainMapper;
using Restaurants.Domains.Models;
using Resturants.Actions.Commands.AddDish;
using Resturants.Domains.DTOs;
using Resturants.Repository.Interfaces;

namespace Resturants.Controllers
{
[Route("endpoints/resturants/{resturant_id}/[controller]")]
[ApiController]
public class DishesController : ControllerBase
{
private readonly IMediator _mediator;

public DishesController(IMediator mediator)
{
_mediator = mediator;
}


[HttpGet]
public async Task<ActionResult<IEnumerable<Dish>>> GetDishes()
{
throw new NotImplementedException();
}


[HttpGet]
[Route("{id:int}")]
public async Task<ActionResult<Dish>> GetDishById(int id)
{
throw new NotImplementedException();
}
private readonly IDishesRepository _repository;


[HttpPut]
[Route("{id:int}")]
public async Task<IActionResult> UpdateDish()
public DishesController(IDishesRepository repository)
{
throw new NotImplementedException();

this._repository = repository;
}


[HttpPost]
public async Task<ActionResult<Dish>> AddDish([FromRoute] int resturant_id, AddDishCommand command)
public async Task<ActionResult<Dish>> AddDish([FromRoute] int resturant_id, AddDishDTO dish_entry)
{
await _mediator.Send(command);
var domain_dish = DataMapper.Instance.Map(dish_entry);

var saved = await _repository.AddDishAsync(domain_dish, CancellationToken.None);

return Created();


return Ok(saved);
}


[HttpDelete]
[Route("{id:int}")]
public async Task<IActionResult> DeleteDish(int id)
{
throw new NotImplementedException();
}

}
}
18 changes: 14 additions & 4 deletions src/DomainMapper/DataMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using Restaurants.Actions.Commands.UpdateResturant;
using Restaurants.Domains.DTOs;
using Restaurants.Domains.Models;
using Resturants.Actions.Commands.AddDish;
using Resturants.Domains.DTOs;

namespace Restaurants.DomainMapper
{
Expand All @@ -15,7 +17,7 @@ private DataMapper()

public static DataMapper Instance => _instance;

public ResturantDTO Mapper(Resturant source) => new()
public ResturantDTO Map(Resturant source) => new()
{
Name = source.Name,
Description = source.Description,
Expand All @@ -25,7 +27,7 @@ private DataMapper()
Email = source.Email
};

public Resturant Mapper(AddResturantCommand add_resturant_command) => new()
public Resturant Map(AddResturantCommand add_resturant_command) => new()
{
Name = add_resturant_command.Name,
Description = add_resturant_command.Description,
Expand All @@ -34,7 +36,7 @@ private DataMapper()
Email = add_resturant_command.Email
};

public Resturant Mapper(AddResturantDTO add_resturant_dto) => new()
public Resturant Map(AddResturantDTO add_resturant_dto) => new()
{
Name = add_resturant_dto.Name,
Description = add_resturant_dto.Description,
Expand All @@ -43,13 +45,21 @@ private DataMapper()
Email = add_resturant_dto.Email
};

public Resturant Mapper(UpdateResturantCommand update_resturant) => new()
public Resturant Map(UpdateResturantCommand update_resturant) => new()
{
Name = update_resturant.Name,
Description = update_resturant.Description,
Category = update_resturant.Category,
PhoneNumber = update_resturant.PhoneNumber,
Email = update_resturant.Email
};

public Dish Map(AddDishDTO command) => new()
{
Name = command.Name,
Description = command.Description,
Price = command.Price,
KiloCalories = command.KiloCalories
};
}
}
13 changes: 13 additions & 0 deletions src/Domains/DTOs/AddDishDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Resturants.Domains.DTOs
{
public class AddDishDTO
{
public string Name { get; set; } = default!;
public string Description { get; set; } = default!;
public decimal Price { get; set; }

public int? KiloCalories { get; set; }

public int RestaurantId { get; set; }
}
}
12 changes: 12 additions & 0 deletions src/Domains/DTOs/DishDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Resturants.Domains.DTOs
{
public class DishDTO
{
public int Id { get; set; }
public string Name { get; set; } = default!;
public string Description { get; set; } = default!;
public decimal Price { get; set; }

public int? KiloCalories { get; set; }
}
}
6 changes: 5 additions & 1 deletion src/Domains/DTOs/ResturantDTO.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Restaurants.Domains.DTOs
using Restaurants.Domains.Models;

namespace Restaurants.Domains.DTOs
{
public class ResturantDTO
{
Expand All @@ -8,5 +10,7 @@ public class ResturantDTO
public bool HasDelivery { get; set; }
public string? PhoneNumber { get; set; }
public string? Email { get; set; }

public List<Dish> Dishes { get; set; } = [];
}
}
4 changes: 2 additions & 2 deletions src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public static async Task Main(string[] args)

await seeder.SeedAsync();

app.UseMiddleware<ErrorHandler>();
app.UseSerilogRequestLogging();
// app.UseMiddleware<ErrorHandler>();
// app.UseSerilogRequestLogging();

if (app.Environment.IsDevelopment())
{
Expand Down
13 changes: 13 additions & 0 deletions src/Properties/serviceDependencies.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"dependencies": {
"mssql1": {
"type": "mssql",
"connectionId": "ConnectionStrings:DatabaseConnection",
"dynamicId": null
},
"secrets1": {
"type": "secrets",
"connectionId": null
}
}
}
16 changes: 16 additions & 0 deletions src/Properties/serviceDependencies.local.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"dependencies": {
"mssql1": {
"secretStore": "LocalSecretsFile",
"type": "mssql.onprem",
"connectionId": "ConnectionStrings:DatabaseConnection",
"dynamicId": null
},
"secrets1": {
"secretStore": null,
"resourceId": null,
"type": "secrets.user",
"connectionId": null
}
}
}
24 changes: 2 additions & 22 deletions src/Repository/Implementations/DishesRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,13 @@ public DishesRepository(ResturantDbContext context)
{
_context = context;
}
public async Task<Dish> AddDishAsync(Dish add_dish, CancellationToken cancellation_token)
public async Task<int> AddDishAsync(Dish add_dish, CancellationToken cancellation_token)
{
_context.Dishes.Add(add_dish);

await _context.SaveChangesAsync();

return add_dish;
}

public Task<bool> DeleteDishAsync(int id, CancellationToken cancellation_token)
{
throw new NotImplementedException();
}

public Task<Dish?> GetDishByIdAsync(int id, CancellationToken cancellation_token)
{
throw new NotImplementedException();
}

public Task<List<Dish>?> GetDishesAsync(CancellationToken cancellation_token)
{
throw new NotImplementedException();
}

public Task<bool> UpdateDishAsync(int id, Dish update_dish, CancellationToken cancellation_token)
{
throw new NotImplementedException();
return add_dish.Id;
}
}
}
2 changes: 0 additions & 2 deletions src/Repository/Implementations/ResturantRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ public async Task<bool> UpdateResturantAsync(int id, Resturant update_resturant)

existing_resturant.Email = update_resturant.Email;



_context.Resturants.Update(existing_resturant);

await _context.SaveChangesAsync();
Expand Down
10 changes: 5 additions & 5 deletions src/Repository/Interfaces/IDishesRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ namespace Resturants.Repository.Interfaces
{
public interface IDishesRepository
{
Task<List<Dish>> GetDishesAsync(CancellationToken cancellation_token);
Task<Dish?> GetDishByIdAsync(int id, CancellationToken cancellation_token);
Task<Dish> AddDishAsync(Dish add_dish, CancellationToken cancellation_token);
Task<bool> DeleteDishAsync(int id, CancellationToken cancellation_token);
Task<bool> UpdateDishAsync(int id, Dish update_dish, CancellationToken cancellation_token);
//Task<List<Dish>?> GetDishesAsync(CancellationToken cancellation_token);
//Task<Dish?> GetDishByIdAsync(int id, CancellationToken cancellation_token);
Task<int> AddDishAsync(Dish add_dish, CancellationToken cancellation_token);
//Task<bool> DeleteDishAsync(int id, CancellationToken cancellation_token);
// Task<bool> UpdateDishAsync(int id, Dish update_dish, CancellationToken cancellation_token);
}
}
1 change: 1 addition & 0 deletions src/Resturants.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>26c268de-0d90-40e2-9620-2dd45614e88e</UserSecretsId>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Seeders/ResturantSeeder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ private static IEnumerable<Resturant> GetResturants()
// converted_domain_resturants.Add(DataMapper.Instance.Mapper(resturant));
//}

dto_resturants.ForEach(r => converted_domain_resturants.Add(DataMapper.Instance.Mapper(r)));
dto_resturants.ForEach(r => converted_domain_resturants.Add(DataMapper.Instance.Map(r)));

return converted_domain_resturants;
}
Expand Down

0 comments on commit 20026bf

Please sign in to comment.