Coupon Service Interface and Coupon Service Implementation - bondaigames/ASP.NET-Core-Documentation GitHub Wiki
- Create CouponDto.cs in Models/Dto folder
using Utility;
using System.Text.Json.Serialization;
namespace Web.Admin.Models.Dto
{
public class CouponDto
{
[JsonPropertyName(PN.CouponId)]
public int CouponId { get; set; }
[JsonPropertyName(PN.CouponCode)]
public string? CouponCode { get; set; }
[JsonPropertyName(PN.DiscountAmount)]
public double DiscountAmount { get; set; }
[JsonPropertyName(PN.MinAmount)]
public int MinAmount { get; set; }
}
}
- Create Interface ICouponService.cs in Service\IService folder
using Web.Admin.Models.Dto;
namespace Web.Admin.Service.IService
{
public interface ICouponService
{
Task<ResponseDto?> GetCouponAsync(string couponCode);
Task<ResponseDto?> GetAllCouponsAsync();
Task<ResponseDto?> GetCouponByIdAsync(int id);
Task<ResponseDto?> CreateCouponsAsync(CouponDto couponDto);
Task<ResponseDto?> UpdateCouponsAsync(CouponDto couponDto);
Task<ResponseDto?> DeleteCouponsAsync(int id);
}
}
- Go to SD.cs and add CouponApiBase Url
public static string CouponAPIBase { get; set; }
- Define url in appsettings.json
"ServicesUrls": {
"CouponAPI": "https://localhost:7001",
}
- Create CouponService.cs in Service folder
using Web.Admin.Models.Dto;
using Web.Admin.Service.IService;
using Web.Admin.Utility;
namespace Web.Admin.Service
{
public class CouponService : ICouponService
{
private readonly IBaseService _baseService;
public CouponService(IBaseService baseService)
{
_baseService = baseService;
}
public async Task<ResponseDto?> CreateCouponsAsync(CouponDto couponDto)
{
return await _baseService.SendAsync(new RequestDto()
{
ApiType = SD.ApiType.POST,
Data = couponDto,
Url = SD.CouponAPIBase + "/api/coupon"
});
}
public async Task<ResponseDto?> DeleteCouponsAsync(int id)
{
return await _baseService.SendAsync(new RequestDto()
{
ApiType = SD.ApiType.DELETE,
Url = SD.CouponAPIBase + "/api/coupon/" + id
});
}
public async Task<ResponseDto?> GetAllCouponsAsync()
{
return await _baseService.SendAsync(new RequestDto()
{
ApiType = SD.ApiType.GET,
Url = SD.CouponAPIBase + "/api/coupon"
});
}
public async Task<ResponseDto?> GetCouponAsync(string couponCode)
{
return await _baseService.SendAsync(new RequestDto()
{
ApiType = SD.ApiType.GET,
Url = SD.CouponAPIBase + "/api/coupon/GetByCode/" + couponCode
});
}
public async Task<ResponseDto?> GetCouponByIdAsync(int id)
{
return await _baseService.SendAsync(new RequestDto()
{
ApiType = SD.ApiType.GET,
Url = SD.CouponAPIBase + "/api/coupon/" + id
});
}
public async Task<ResponseDto?> UpdateCouponsAsync(CouponDto couponDto)
{
return await _baseService.SendAsync(new RequestDto()
{
ApiType = SD.ApiType.PUT,
Data = couponDto,
Url = SD.CouponAPIBase + "/api/coupon"
});
}
}
}