Esempio n. 1
0
        public async Task <Request> CreateRequest(SellShareViewModel viewModel)
        {
            try
            {
                //TODO: Check if the user has multiple requests with the same shareId in progress
                if (!await _shareControlService.HasEnoughShares(viewModel.PortfolioId, viewModel.ShareId,
                                                                viewModel.Amount))
                {
                    throw new ArgumentException("Owner porfolio doesn't have enough shares");
                }
            }
            catch (Exception e)
            {
                throw new ArgumentException("Something went wrong, try again");
            }

            var request = new Request
            {
                RequestId      = Guid.NewGuid(),
                ShareId        = viewModel.ShareId,
                OwnerAccountId = viewModel.AccountId,
                PortfolioId    = viewModel.PortfolioId,
                Amount         = viewModel.Amount,
                Status         = "open",
                DateAdded      = DateTime.UtcNow
            };

            _unitOfWork.RequestsRepository.Add(request);
            await _unitOfWork.CommitAsync();

            return(request);
        }
Esempio n. 2
0
        public async Task <IActionResult> SellShare([FromBody] SellShareViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var request = await _traderService.CreateRequest(viewModel);

            return(CreatedAtAction(nameof(GetRequestById), new { request.RequestId }, request));
        }
Esempio n. 3
0
        public async Task <IActionResult> SellShare([FromBody] SellShareViewModel sellShare)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            try
            {
                var request = await _sellerService.SellShare(sellShare.UserId,
                                                             sellShare.ShareId,
                                                             sellShare.Amount);

                if (request == null)
                {
                    return(BadRequest("Something went wrong. Try again later"));
                }
                return(Ok(request));
            }
            catch (System.Exception)
            {
                return(BadRequest(new { Status = "failure" }));
            }
        }