Ejemplo n.º 1
0
        public ActionResult <ClaimedVoucher> PostClaimedVoucher(ClaimedVoucherCreateRequest entity)
        {
            bool success = _claimedSer.Create(entity);

            if (success)
            {
                return(Ok(entity));
            }
            return(Problem("Create failed!"));
        }
Ejemplo n.º 2
0
        public bool Create(ClaimedVoucherCreateRequest entity)
        {
            int     userId    = entity.UserId;
            int     voucherId = entity.VoucherId;
            int     available = entity.Available;
            Voucher voucher   = _vouRepo.GetAll().FirstOrDefault(e => e.Id == voucherId);

            if (available <= 0 || available > voucher.Available)
            {
                return(false);
            }
            ClaimedVoucher existed = _claimedRepo.GetAll().FirstOrDefault(e => e.UserId == userId && e.VoucherId == voucherId);

            if (existed != null)
            {
                existed.Available += available;
                return(_claimedRepo.Update(existed));
            }

            ClaimedVoucher newEntity = new ClaimedVoucher();

            newEntity.Available   = available;
            newEntity.ClaimedDate = DateTime.Now;
            newEntity.ExpiredDate = DateTime.Now.AddDays(30);
            newEntity.UserId      = userId;
            newEntity.VoucherId   = voucherId;
            bool success = _claimedRepo.Create(newEntity);

            //update voucher available
            if (success)
            {
                voucher.Available -= available;
                success            = _vouRepo.Update(voucher);
            }

            return(success);
        }