Skip to content
Merged
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
15 changes: 15 additions & 0 deletions Dentizone.Application/Interfaces/Order/IShippingService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dentizone.Domain.Enums;

namespace Dentizone.Application.Interfaces.Order
{
public interface IShippingService
{
Task UpdateItemShipmentStatusAsync(string orderItemId, ShipmentActivityStatus newStatus,
string? comments);
}
}
63 changes: 63 additions & 0 deletions Dentizone.Application/Services/ShippingService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Dentizone.Application.Interfaces;
using Dentizone.Application.Interfaces.Order;
using Dentizone.Application.Services.Authentication;
using Dentizone.Domain.Entity;
using Dentizone.Domain.Enums;
using Dentizone.Domain.Exceptions;
using Dentizone.Domain.Interfaces.Mail;
using Dentizone.Domain.Interfaces.Repositories;
using Microsoft.Extensions.Hosting;

namespace Dentizone.Application.Services
{
internal class ShippingService(
IOrderItemRepository orderItemRepository,
IShipmentActivityRepository shipmentActivityRepository,
IMailService mailService,
AuthService authService)
: IShippingService
{
public async Task UpdateItemShipmentStatusAsync(string orderItemId, ShipmentActivityStatus newStatus,
string? comments)
{
//search in DataBase if orderItemId found or not?

var item = await orderItemRepository.FindBy(
oi => oi.Id == orderItemId,
[oi => oi.ShipmentActivities]);

if (item == null)
{
throw new NotFoundException("Not Found");
}
else
{
Comment on lines +35 to +40
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve exception message and remove unnecessary else block.

Use a more descriptive exception message and remove the redundant else block after throw.

             if (item == null)
             {
-                throw new NotFoundException("Not Found");
+                throw new NotFoundException($"Order item with ID '{orderItemId}' was not found");
             }
-            else
-            {

Also remove the corresponding closing brace at line 61:

-            }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (item == null)
{
throw new NotFoundException("Not Found");
}
else
{
if (item == null)
{
throw new NotFoundException($"Order item with ID '{orderItemId}' was not found");
}
🤖 Prompt for AI Agents
In Dentizone.Application/Services/ShippingService.cs between lines 35 and 40,
replace the generic "Not Found" message in the NotFoundException with a more
descriptive message indicating what was not found. Remove the else block
following the throw statement since it is redundant, and also remove the
corresponding closing brace at line 61 to maintain proper code structure.

var shipmentActivity = new ShipmentActivity
{
ItemId = orderItemId,
Status = newStatus,
ActivityDescription = comments
};
await shipmentActivityRepository.CreateAsync(shipmentActivity);


var seller = await authService.GetById(item.Post.SellerId);


await mailService.Send(seller.Email, $"the Status has been changed to {newStatus}",
"New status update");

var buyer = await authService.GetById(item.Order.BuyerId);

await mailService.Send(buyer.Email, $"the Status has been changed to {newStatus}",
"New status update");
}
}
}
}
21 changes: 21 additions & 0 deletions Dentizone.Presentaion/Controllers/ShippingController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Dentizone.Application.Interfaces.Order;
using Dentizone.Domain.Enums;
using Dentizone.Domain.Interfaces.Repositories;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Dentizone.Presentaion.Controllers
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix typo in namespace.

The namespace contains a typo: "Presentaion" should be "Presentation".

-namespace Dentizone.Presentaion.Controllers
+namespace Dentizone.Presentation.Controllers
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
namespace Dentizone.Presentaion.Controllers
namespace Dentizone.Presentation.Controllers
🤖 Prompt for AI Agents
In Dentizone.Presentaion/Controllers/ShippingController.cs at line 7, correct
the typo in the namespace declaration by changing
"Dentizone.Presentaion.Controllers" to "Dentizone.Presentation.Controllers" to
fix the spelling error.

{
[Route("api/[controller]")]
[ApiController]
public class ShippingController(IShippingService shipmentActivity) : ControllerBase
{
[HttpPut]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add route template to the HttpPut attribute.

The PUT endpoint should have a route template to properly define the API path.

-        [HttpPut]
+        [HttpPut("update-status")]
🤖 Prompt for AI Agents
In Dentizone.Presentaion/Controllers/ShippingController.cs at line 13, the
HttpPut attribute lacks a route template, which is necessary to define the API
endpoint path. Add a route template string inside the HttpPut attribute, for
example [HttpPut("your-route")], to specify the URL path this PUT method will
handle.

public async Task<IActionResult> UpdateItemShipmentStatus(string orderItemId, ShipmentActivityStatus newStatus,
string? comment)
{
await shipmentActivity.UpdateItemShipmentStatusAsync(orderItemId, newStatus, comment);
return Ok();
}
Comment on lines +14 to +19
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add error handling and return response data.

The endpoint should handle potential exceptions and return meaningful response data to the client.

         public async Task<IActionResult> UpdateItemShipmentStatus(string orderItemId, ShipmentActivityStatus newStatus,
             string? comment)
         {
-            await shipmentActivity.UpdateItemShipmentStatusAsync(orderItemId, newStatus, comment);
-            return Ok();
+            try
+            {
+                await shipmentActivity.UpdateItemShipmentStatusAsync(orderItemId, newStatus, comment);
+                return Ok(new { orderItemId, status = newStatus, message = "Shipment status updated successfully" });
+            }
+            catch (NotFoundException ex)
+            {
+                return NotFound(new { error = ex.Message });
+            }
+            catch (Exception ex)
+            {
+                // Log the exception
+                return StatusCode(500, new { error = "An error occurred while updating shipment status" });
+            }
         }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public async Task<IActionResult> UpdateItemShipmentStatus(string orderItemId, ShipmentActivityStatus newStatus,
string? comment)
{
await shipmentActivity.UpdateItemShipmentStatusAsync(orderItemId, newStatus, comment);
return Ok();
}
public async Task<IActionResult> UpdateItemShipmentStatus(string orderItemId, ShipmentActivityStatus newStatus,
string? comment)
{
try
{
await shipmentActivity.UpdateItemShipmentStatusAsync(orderItemId, newStatus, comment);
return Ok(new { orderItemId, status = newStatus, message = "Shipment status updated successfully" });
}
catch (NotFoundException ex)
{
return NotFound(new { error = ex.Message });
}
catch (Exception ex)
{
// Log the exception
return StatusCode(500, new { error = "An error occurred while updating shipment status" });
}
}
🤖 Prompt for AI Agents
In Dentizone.Presentaion/Controllers/ShippingController.cs around lines 14 to
19, the UpdateItemShipmentStatus method lacks error handling and does not return
any response data. Wrap the call to
shipmentActivity.UpdateItemShipmentStatusAsync in a try-catch block to handle
potential exceptions. On success, return a meaningful success response, and on
failure, catch exceptions and return an appropriate error response with relevant
details to the client.

}
}
Loading