I have created a mvc application for booking space. In this for booking user can applied coupon for booking.
I have implemented one coupon service and validate coupon.
public class Coupon { public int ID { get; set; } public string Name { get; set; } public string Description { get; set; } public Enum.Enum_DiscountType DiscountType { get; set; } public Enum_DiscountTypeAppliedOn DiscountTypeAppliedOn { get; set; } public decimal DiscountValue { get; set; } public decimal? MaximumDiscountAmount { get; set; } public string CouponCode { get; set; } public DateTimeOffset StartDate { get; set; } public DateTimeOffset EndDate { get; set; } public Enum_CouponLimitationType CouponLimitationType { get; set; } public int? LimitationTimes { get; set; } public bool Status { get; set; } public Enum_CouponStatus CouponStatus { get { return Status ? Enum_CouponStatus.Active: Enum_CouponStatus.InActive; } } public Enum_CouponAssignedTo CouponAssignedTo { get; set; } public string Postcodes { get; set; } public bool UseMaximumDiscountAmount { get; set; } public virtual ICollection<CouponUsageHistory> CouponUsageHistory { get; set; } public virtual ICollection<Order> Orders { get; set; } }
My coupon service contains below class for validation
public async Task<bool> IsAppliedCouponValid(Coupon coupon, string userID, Order order, int? categoryID) { if (coupon == null) return false; if (coupon.Status == false) return false; //check date range var now = DateTime.UtcNow; var startDate = coupon.StartDate; if (startDate.CompareTo(now) > 0) return false; var endDate = coupon.EndDate; if (endDate.CompareTo(now) < 0) return false; //check coupon applied limitations var isValid = await CheckCouponLimitations(coupon, userID); if (!isValid) return false; return true; }
CheckCouponLimitations method contains
private async Task<bool> CheckCouponLimitations(Coupon coupon, string userID) { if (coupon == null) throw new ArgumentNullException("coupon"); switch (coupon.CouponLimitationType) { case Model.Enum.Enum_CouponLimitationType.Unlimited: { return true; } case Model.Enum.Enum_CouponLimitationType.NTimesOnly: { var totalUsage = await GetAllCouponUsageHistory(coupon.ID); return totalUsage.Count < coupon.LimitationTimes.Value; } case Model.Enum.Enum_CouponLimitationType.NTimesPerCustomer: { //registered customer var totalUsage = await GetAllCouponUsageHistory(coupon.ID, userID); return totalUsage.Count < coupon.LimitationTimes.Value; } default: break; } return false; }
I have implemented like above and also calculate order after applied coupon also generate new coupon dynamically.
Please suggest me to best way to design this code and give some feedback for review code.