Example #1
0
        public PurchaseTransactionDto CreateTrans(long id, PurchaseTransactionDto purchaseTransaction)
        {
            var user = _context.Users.FirstOrDefault(x => x.Id == id);

            if (user == null)
            {
                return(null);
            }

            var postTrans = mapper.Map <PurchaseTransaction>(purchaseTransaction);

            _context.PurchaseTransactions.Add(postTrans);
            _context.SaveChanges();

            purchaseTransaction = mapper.Map <PurchaseTransactionDto>(postTrans);

            // if member’s birth month falls into transact month
            if (user.Dob.Month == postTrans.TransactDateTime.Month)
            {
                //add 10 points to user
                user.Point = user.Point.HasValue ? user.Point += _appSettings.BirthmonthPoint : user.Point = _appSettings.BirthmonthPoint;
                _context.SaveChanges();
                // give Rp. 100 value voucher with 3 months of validity
                purchaseTransaction.Voucher = CreateVoucher(postTrans.TransactDateTime,
                                                            postTrans.TransactDateTime.AddMonths(_appSettings.VoucherValidity),
                                                            _appSettings.VoucherValue);
            }
            ;

            return(purchaseTransaction);
        }
        public IActionResult CreateTrans(PurchaseTransactionDto purchaseTransaction)
        {
            var userId = Convert.ToInt64(HttpContext.User.Identity.Name);

            if (!ModelState.IsValid || userId == 0)
            {
                return(BadRequest());
            }

            var createdTransaction = _userService.CreateTrans(userId, purchaseTransaction);

            if (createdTransaction == null)
            {
                return(BadRequest());
            }

            return(Ok(createdTransaction));
        }