Beispiel #1
0
        public static ClaimedVoucherDto FromDomain(ClaimedVoucher claimedVoucher)
        {
            var dto = new ClaimedVoucherDto()
            {
                Id          = claimedVoucher.Id,
                VoucherType = (VoucherTypeEnum)((int)claimedVoucher.VoucherType),
                ClaimedOn   = claimedVoucher.ClaimedOn
            };

            return(dto);
        }
Beispiel #2
0
        public bool Update(ClaimedVoucherUpdateRequest entity)
        {
            int            available = entity.Available;
            ClaimedVoucher existed   = _claimedRepo.GetById(entity.Id);

            if (existed == null)
            {
                return(false);
            }
            if (available == existed.Available)
            {
                return(false);
            }
            existed.Available    = available;
            existed.LastUsedDate = DateTime.Now;

            return(_claimedRepo.Update(existed));
        }
Beispiel #3
0
        public bool DecreaseAvailableByOne(int id)
        {
            ClaimedVoucher entity = _claimedRepo.GetById(id);

            if (entity != null)
            {
                bool success;
                entity.Available -= 1;
                if (entity.Available > 0)
                {
                    success = _claimedRepo.Update(entity);
                }
                else
                {
                    success = _claimedRepo.Delete(entity);
                }
                return(success);
            }
            return(false);
        }
Beispiel #4
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);
        }
Beispiel #5
0
        public bool Update(ClaimedVoucher entity)
        {
            _context.Entry(entity).State = EntityState.Modified;

            return(_context.SaveChanges() >= 0);
        }
Beispiel #6
0
        public bool Delete(ClaimedVoucher entity)
        {
            _context.ClaimedVouchers.Remove(entity);

            return(_context.SaveChanges() >= 0);
        }
Beispiel #7
0
        public bool Create(ClaimedVoucher entity)
        {
            _context.ClaimedVouchers.Add(entity);

            return(_context.SaveChanges() >= 0);
        }