Example #1
0
        public async Task <ActionResult <CouponDto> > GetCoupon(int couponID)
        {
            var coupon = await couponRepository.GetCouponAsync(couponID);

            var dtoToReturn = mapper.Map <CouponDto>(coupon);

            return(Ok(dtoToReturn));
        }
Example #2
0
        public async Task ExecuteAsync(ApplyCouponCommand message, CancellationToken token)
        {
            var coupon = await _couponRepository.GetCouponAsync(message.Coupon, token);

            var tutor = await _tutorRepository.LoadAsync(message.TutorId, token);

            if (coupon is null)
            {
                throw new ArgumentException("invalid coupon");
            }

            if (coupon.Tutor != null && coupon.Tutor.Id != message.TutorId)
            {
                throw new ArgumentException("invalid coupon");
            }
            var user = await _userRepository.LoadAsync(message.UserId, token);

            user.ApplyCoupon(coupon, tutor);


            var tutorPrice = tutor.Price.Price;

            message.NewPrice = Coupon.CalculatePrice(coupon.CouponType, tutorPrice, coupon.Value);
        }
        public async Task <ActionResult <IEnumerable <CouponUserDto> > > GetCouponsOfUser([FromQuery] CouponUserDtoParameter parameter)
        {
            Guid userID = new Guid(User.FindFirst(ClaimTypes.NameIdentifier)?.Value);

            if (!await userRepository.UserExistsAsync(userID))
            {
                return(NotFound("用户不存在!"));
            }
            IEnumerable <Coupon_User> result;
            IQueryable <Coupon_User>  queryable = couponRepository.GetIQueryableCouponUser()
                                                  .Include(c => c.Coupon);

            DateTime now = DateTime.Now;

            switch (parameter.Type)
            {
            case CouponsType.Available:
                var coupons = await couponRepository.GetIQueryableCoupon().Include(c => c.Users).ThenInclude(u => u.Coupon).Where(c => c.StartTime <= now && c.EndTime >= now && !c.IsDeleted).OrderBy(c => c.Id).ToListAsync();

                List <Coupon_User> couponUsers = new(16);
                foreach (var coupon in coupons)
                {
                    var u = coupon.Users.Where(u => u.UserID.Equals(userID))
                            .FirstOrDefault();

                    if (u != null)
                    {
                        couponUsers.Add(u);
                    }
                    else
                    {
                        couponUsers.Add(new Coupon_User
                        {
                            UserID        = userID,
                            CouponID      = coupon.Id,
                            Coupon        = await couponRepository.GetCouponAsync(coupon.Id),
                            RecievedCount = 0,
                            RemainedCount = 0
                        });
                    }
                }
                result = couponUsers;
                break;

            case CouponsType.Expired:
                result = await queryable.Where(c => c.UserID.Equals(userID) &&
                                               c.Coupon.EndTime < now &&
                                               !c.Coupon.IsDeleted)
                         .ToListAsync();

                break;

            case CouponsType.Owned:
                result = await queryable.Where(c => c.UserID.Equals(userID) &&
                                               c.Coupon.StartTime <= now &&
                                               c.Coupon.EndTime >= now)
                         .ToListAsync();

                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(parameter.Type));
            }

            var dtoToReturn = mapper.Map <IEnumerable <CouponUserDto> >(result);

            return(Ok(dtoToReturn));
        }