Implement complete e-commerce functionality with shipping and order management
Features Added: - Standard e-commerce properties (Price, Weight, shipping fields) - Order management with Create/Edit views and shipping information - ShippingRates system for weight-based shipping calculations - Comprehensive test coverage with JWT authentication tests - Sample data seeder with 5 orders demonstrating full workflow - Photo upload functionality for products - Multi-cryptocurrency payment support (BTC, XMR, USDT, etc.) Database Changes: - Added ShippingRates table - Added shipping fields to Orders (Name, Address, City, PostCode, Country) - Renamed properties to standard names (BasePrice to Price, ProductWeight to Weight) - Added UpdatedAt timestamps to models UI Improvements: - Added Create/Edit views for Orders - Added ShippingRates management UI - Updated navigation menu with Shipping option - Enhanced Order Details view with shipping information Sample Data: - 3 Categories (Electronics, Clothing, Books) - 5 Products with various prices - 5 Shipping rates (Royal Mail options) - 5 Orders in different statuses (Pending to Delivered) - 3 Crypto payments demonstrating payment flow Security: - All API endpoints secured with JWT authentication - No public endpoints - client apps must authenticate - Privacy-focused design with minimal data collection Generated with Claude Code Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
142
LittleShop/Services/ShippingRateService.cs
Normal file
142
LittleShop/Services/ShippingRateService.cs
Normal file
@@ -0,0 +1,142 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using LittleShop.Data;
|
||||
using LittleShop.Models;
|
||||
using LittleShop.DTOs;
|
||||
|
||||
namespace LittleShop.Services;
|
||||
|
||||
public interface IShippingRateService
|
||||
{
|
||||
Task<IEnumerable<ShippingRateDto>> GetAllShippingRatesAsync();
|
||||
Task<ShippingRateDto?> GetShippingRateByIdAsync(Guid id);
|
||||
Task<ShippingRateDto> CreateShippingRateAsync(CreateShippingRateDto dto);
|
||||
Task<bool> UpdateShippingRateAsync(Guid id, UpdateShippingRateDto dto);
|
||||
Task<bool> DeleteShippingRateAsync(Guid id);
|
||||
Task<ShippingRateDto?> CalculateShippingAsync(decimal weight, string country);
|
||||
}
|
||||
|
||||
public class ShippingRateService : IShippingRateService
|
||||
{
|
||||
private readonly LittleShopContext _context;
|
||||
private readonly ILogger<ShippingRateService> _logger;
|
||||
|
||||
public ShippingRateService(LittleShopContext context, ILogger<ShippingRateService> logger)
|
||||
{
|
||||
_context = context;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<ShippingRateDto>> GetAllShippingRatesAsync()
|
||||
{
|
||||
var rates = await _context.ShippingRates
|
||||
.OrderBy(sr => sr.Country)
|
||||
.ToListAsync();
|
||||
|
||||
// Sort by MinWeight in memory to avoid SQLite decimal ordering issue
|
||||
return rates.OrderBy(sr => sr.MinWeight).Select(MapToDto);
|
||||
}
|
||||
|
||||
public async Task<ShippingRateDto?> GetShippingRateByIdAsync(Guid id)
|
||||
{
|
||||
var rate = await _context.ShippingRates.FindAsync(id);
|
||||
return rate == null ? null : MapToDto(rate);
|
||||
}
|
||||
|
||||
public async Task<ShippingRateDto> CreateShippingRateAsync(CreateShippingRateDto dto)
|
||||
{
|
||||
var rate = new ShippingRate
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Name = dto.Name,
|
||||
Description = dto.Description,
|
||||
Country = dto.Country,
|
||||
MinWeight = dto.MinWeight,
|
||||
MaxWeight = dto.MaxWeight,
|
||||
Price = dto.Price,
|
||||
MinDeliveryDays = dto.MinDeliveryDays,
|
||||
MaxDeliveryDays = dto.MaxDeliveryDays,
|
||||
IsActive = dto.IsActive,
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
UpdatedAt = DateTime.UtcNow
|
||||
};
|
||||
|
||||
_context.ShippingRates.Add(rate);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
_logger.LogInformation("Created shipping rate {RateId} for {Country}: {Name}",
|
||||
rate.Id, rate.Country, rate.Name);
|
||||
|
||||
return MapToDto(rate);
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateShippingRateAsync(Guid id, UpdateShippingRateDto dto)
|
||||
{
|
||||
var rate = await _context.ShippingRates.FindAsync(id);
|
||||
if (rate == null)
|
||||
return false;
|
||||
|
||||
rate.Name = dto.Name;
|
||||
rate.Description = dto.Description;
|
||||
rate.Country = dto.Country;
|
||||
rate.MinWeight = dto.MinWeight;
|
||||
rate.MaxWeight = dto.MaxWeight;
|
||||
rate.Price = dto.Price;
|
||||
rate.MinDeliveryDays = dto.MinDeliveryDays;
|
||||
rate.MaxDeliveryDays = dto.MaxDeliveryDays;
|
||||
rate.IsActive = dto.IsActive;
|
||||
rate.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
_logger.LogInformation("Updated shipping rate {RateId}", id);
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteShippingRateAsync(Guid id)
|
||||
{
|
||||
var rate = await _context.ShippingRates.FindAsync(id);
|
||||
if (rate == null)
|
||||
return false;
|
||||
|
||||
_context.ShippingRates.Remove(rate);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
_logger.LogInformation("Deleted shipping rate {RateId}", id);
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<ShippingRateDto?> CalculateShippingAsync(decimal weight, string country)
|
||||
{
|
||||
// Convert weight to grams for comparison
|
||||
var weightInGrams = weight * 1000; // Assuming weight is in kg
|
||||
|
||||
var rate = await _context.ShippingRates
|
||||
.Where(sr => sr.IsActive
|
||||
&& sr.Country.ToLower() == country.ToLower()
|
||||
&& sr.MinWeight <= weightInGrams
|
||||
&& sr.MaxWeight >= weightInGrams)
|
||||
.OrderBy(sr => sr.Price)
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
return rate == null ? null : MapToDto(rate);
|
||||
}
|
||||
|
||||
private static ShippingRateDto MapToDto(ShippingRate rate)
|
||||
{
|
||||
return new ShippingRateDto
|
||||
{
|
||||
Id = rate.Id,
|
||||
Name = rate.Name,
|
||||
Description = rate.Description,
|
||||
Country = rate.Country,
|
||||
MinWeight = rate.MinWeight,
|
||||
MaxWeight = rate.MaxWeight,
|
||||
Price = rate.Price,
|
||||
MinDeliveryDays = rate.MinDeliveryDays,
|
||||
MaxDeliveryDays = rate.MaxDeliveryDays,
|
||||
IsActive = rate.IsActive,
|
||||
CreatedAt = rate.CreatedAt,
|
||||
UpdatedAt = rate.UpdatedAt
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user