public void DeleteCouponUser(Coupon_User coupon_user) { if (coupon_user == null) { throw new ArgumentNullException(nameof(coupon_user)); } context.Coupon_Users.Remove(coupon_user); }
public void AddCouponUser(Coupon_User coupon_user) { if (coupon_user == null) { throw new ArgumentNullException(nameof(coupon_user)); } context.Coupon_Users.Add(coupon_user); }
public async Task <Coupon_User> AddCouponToUserAsync(int couponID, Guid userID) { var optionsBuilder = new DbContextOptionsBuilder <MallDbContext>(); optionsBuilder.UseMySql(configuration.GetConnectionString("DefaultConnection")); using (MallDbContext mContext = new MallDbContext(optionsBuilder.Options)) { var coupon = await mContext.Coupons.FindAsync(couponID); if (coupon == null) { return(null); } if (coupon.CouponCount <= 0) { return(null); } var couponUser = await mContext.Coupon_Users .Include(c => c.Coupon) .Where(c => c.CouponID == couponID && c.UserID.Equals(userID)) .FirstOrDefaultAsync(); if (couponUser == null) { coupon.CouponCount--; couponUser = new Coupon_User { CouponID = couponID, UserID = userID, RecievedCount = 1, RemainedCount = 1 }; mContext.Coupon_Users.Add(couponUser); try { await mContext.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { return(null); } } else { if (couponUser.RecievedCount >= coupon.AllowLimit) { return(null); } coupon.CouponCount--; couponUser.RecievedCount++; couponUser.RemainedCount++; try { await mContext.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { return(null); } } return(couponUser); } }
public void UpdateCouponUser(Coupon_User coupon_user) { //no need code }