Ejemplo n.º 1
0
        public ActionResult <FollowDto> CreateFollow([FromBody] FollowCreateDto follow)
        {
            try
            {
                if (userMockService.GetAccounByUserId(follow.FollowedId) == null)
                {
                    return(NotFound("User with specified id does not exist."));
                }

                if (blackListMockService.IsUserBlocked(follow.FollowerId, follow.FollowedId))
                {
                    return(StatusCode(StatusCodes.Status406NotAcceptable, "You cannot follow users that you have blocked or who have blocked you."));
                }

                Follow followEntity = mapper.Map <Follow>(follow);

                Follow createdFollow = followRepository.CreateFollow(followEntity);

                var valid = followValidator.Validate(createdFollow);
                if (!valid.IsValid)
                {
                    return(BadRequest(valid.Errors));
                }

                followRepository.SaveChanges();

                logger.Log(LogLevel.Information, $"requestId: {Request.HttpContext.TraceIdentifier}, previousRequestId:No previous ID, Message: Follow successfully created.");

                string location = linkGenerator.GetPathByAction("GetFollow", "Follow", new { followId = createdFollow.FollowId });

                return(Created(location, mapper.Map <FollowDto>(createdFollow)));
            }
            catch (Exception e)
            {
                logger.Log(LogLevel.Warning, $"requestId: {Request.HttpContext.TraceIdentifier}, previousRequestId:No previous ID, Message: Follow unsuccessfully created.", e);
                return(StatusCode(StatusCodes.Status500InternalServerError, "Create error " + e.Message));
            }
        }
Ejemplo n.º 2
0
        public ActionResult <TransactionDto> CreateTransaction([FromBody] TransactionCreateDto transaction)
        {
            try
            {
                var product = productMockService.GetProductById(transaction.ProductId);

                if (product == null)
                {
                    return(NotFound("Product with specified id does not exist."));
                }

                if (!productMockService.HasEnoughProducts(transaction.ProductId, transaction.ProductsQuantity))
                {
                    return(StatusCode(StatusCodes.Status406NotAcceptable, "There is no enough products."));
                }

                if (!transportTypeMockService.TransportTypeExistsById(transaction.TransportTypeId))
                {
                    return(NotFound("Transport type with specified id does not exist."));
                }


                Transaction transactionEntity = mapper.Map <Transaction>(transaction);

                transactionEntity.BuyingDateTime = DateTime.Now;

                Transaction createdTransaction = transactionRepository.CreateTransaction(transactionEntity);
                var         valid = transactionValidator.Validate(createdTransaction);
                if (!valid.IsValid)
                {
                    return(BadRequest(valid.Errors));
                }

                //Trazimo usera da bismo dobili id naloga sa kog treba da skinemo novac
                var user = userMockService.GetAccounByUserId(transaction.BuyerId);

                if (user == null)
                {
                    return(NotFound("User with specified id does not exist."));
                }

                //Trazimo nalog sa kog treba da skinemo novac
                var account = accountMockService.getAccountById(user.AccountId);

                //Iznos koji treba da skinemo sa naloga
                decimal amount = product.Price * transaction.ProductsQuantity;

                if (account == null)
                {
                    return(NotFound("User does not have account."));
                }

                if (account.AccountBalance < amount)
                {
                    return(StatusCode(StatusCodes.Status406NotAcceptable, "You do not have enough money to buy this product."));
                }

                TransactionChargeDto charge = new TransactionChargeDto {
                    AccountId = account.AccountId, Amount = amount
                };

                //Naplacujemo
                bool charged = accountMockService.charge(charge);

                //Proveravamo da li je naplaceno
                if (!charged)
                {
                    return(StatusCode(StatusCodes.Status500InternalServerError, "An error occurred while charging, contact administration"));
                }

                //Smanjujemo broj proizvoda na lageru
                TransactionReduceStockDto purchase = new TransactionReduceStockDto {
                    ProductId = transaction.ProductId, Quantity = transaction.ProductsQuantity
                };

                bool reducedStock = productMockService.ProductPurchased(purchase);

                if (!reducedStock)
                {
                    return(StatusCode(StatusCodes.Status500InternalServerError, "An error occurred while stock reducing, contact administration"));
                }

                transactionRepository.SaveChanges();

                logger.Log(LogLevel.Information, $"requestId: {Request.HttpContext.TraceIdentifier}, previousRequestId:No previous ID, Message: Transaction successfully created.");

                string location = linkGenerator.GetPathByAction("GetTransaction", "Transaction", new { transactionId = createdTransaction.TransactionId });

                return(Created(location, mapper.Map <TransactionDto>(createdTransaction)));
            }
            catch (Exception e)
            {
                logger.Log(LogLevel.Warning, $"requestId: {Request.HttpContext.TraceIdentifier}, previousRequestId:No previous ID, Message: Transaction unsuccessfully created.", e);
                return(StatusCode(StatusCodes.Status500InternalServerError, "Create error " + e.Message));
            }
        }