public async Task <IActionResult> AddCouponToUser(int couponID) { Guid userID = new Guid(User.FindFirst(ClaimTypes.NameIdentifier)?.Value); if (!await userRepository.UserExistsAsync(userID)) { return(UnprocessableEntity("User not exist!")); } var coupon = await couponRepository.GetCouponAsync(couponID); if (coupon == null || coupon.IsDeleted) { return(UnprocessableEntity("Coupon not exist!")); } if (coupon.CouponCount <= 0) { return(BadRequest("Coupon is not enough")); } var couponUser = await couponRepository.AddCouponToUserAsync(couponID, userID); if (couponUser == null) { return(Conflict("Get coupon failed, maybe try again later")); } var dtoToReturn = mapper.Map <CouponUserDto>(couponUser); return(CreatedAtRoute(nameof(GetCouponOfUser), new { couponID, userID }, dtoToReturn)); }