Ejemplo n.º 1
0
        public async Task <IActionResult> GetAllAsync()
        {
            var user = await _userService.GetUserFromPrincipalAsync(this.User);

            var shareList = await _shareService.GetSharesByUserAsync(user);

            var shareDTOList = new List <ShareDTO>();

            foreach (var share in shareList)
            {
                var sharedObjectsList = await _shareService.GetShareContentAsync(share);

                var fsoList = await _fsoService.GetFsoListByIdAsync(sharedObjectsList.Select(x => x.FsoId).ToArray());

                var fsoDTOList = _fsoService.ToDTO(fsoList).OrderBy(f => f.Name).OrderByDescending(f => f.IsFolder);
                foreach (var fsoDTO in fsoDTOList)
                {
                    if (fsoDTO.IsFolder)
                    {
                        await _fsoService.SetContentOfDTO(fsoDTO);
                    }
                }
                var shareDTO = new ShareDTO(share);
                shareDTO.Content = fsoDTOList.ToList();
                shareDTOList.Add(shareDTO);
            }
            return(new JsonResult(shareDTOList));
        }
Ejemplo n.º 2
0
 public static ShareDTO getInstance()
 {
     if (shareDTO == null)
     {
         shareDTO = new ShareDTOImplementation();
     }
     return(shareDTO);
 }
Ejemplo n.º 3
0
        public void AddShare(ShareDTO share, StatisticsDTO statistics)
        {
            var ns = Mapper.Map <Share>(share);

            repo.Add(ns);
            statistics.InsertsTime += repo.LastOperationTime;
            statistics.InsertsCount++;
        }
Ejemplo n.º 4
0
        public ShareDTO AddShare(ShareDTO _share)
        {
            var share = _context.Shares.FirstOrDefault(sh => sh.FileId == _share.FileId && sh.ToUserId == _share.ToUserId && sh.OwnerId == _share.OwnerId);

            if (share != null)
            {
                throw new Exception("Данная связь уже существует");
            }
            //var stat = _context.Statistics.First(st => st.UserId == _share.OwnerId);
            //stat.ShareCount++;
            _context.Shares.Add(_share.Map());
            _context.SaveChanges();
            return(new ShareDTO(_share.Map()));
        }
Ejemplo n.º 5
0
        public bool EditShare(int id, ShareDTO share, StatisticsDTO statistics)
        {
            var edited = repo.GetById(id);

            statistics.SelectsTime += repo.LastOperationTime;
            statistics.SelectsCount++;

            if (edited == null)
            {
                return(false);
            }

            edited.OwnerId = share.OwnerId;
            edited.StockId = share.StockId;
            edited.Amount  = share.Amount;

            repo.Edit(edited);
            statistics.UpdatesTime += repo.LastOperationTime;
            statistics.UpdatesCount++;

            return(true);
        }
Ejemplo n.º 6
0
        public void FindSellOffers(BuyOfferDTO buyOffer, UserDTO currentUser, StatisticsDTO statistics)
        {
            if (currentUser.Value < buyOffer.Amount * buyOffer.Price)
            {
                return;
            }

            var offers = _sellOfferService.GetAll(statistics).Where(o => o.Price <= buyOffer.Price).OrderBy(o => o.Price);

            foreach (var offer in offers)
            {
                int tradedAmount;

                if (buyOffer.Amount < offer.Amount)
                {
                    var price = offer.Amount * offer.Price;

                    offer.Amount -= buyOffer.Amount;
                    tradedAmount  = buyOffer.Amount;
                    var targetUser = _userService.GetUserById(offer.SellerId, statistics);

                    //money got exchanged
                    targetUser.Value    += price;
                    currentUser.Value   -= price;
                    currentUser.Password = null;
                    targetUser.Password  = null;
                    _userService.EditUser(currentUser.Id, currentUser, statistics);
                    _userService.EditUser(targetUser.Id, targetUser, statistics);

                    //shares were taken from source
                    var targetedShare = _shareService.GetShareById(offer.ShareId, statistics);
                    targetedShare.Amount -= buyOffer.Amount;
                    _shareService.EditShare(targetedShare.Id, targetedShare, statistics);

                    //if current user has company share add them if not create entry for them
                    var currentUserShare = _shareService.GetAllShares(statistics).Where(c => c.OwnerId == currentUser.Id).Where(c => c.StockId == buyOffer.StockId).FirstOrDefault();
                    if (currentUserShare != null)
                    {
                        currentUserShare.Amount += buyOffer.Amount;
                        _shareService.EditShare(currentUserShare.Id, currentUserShare, statistics);
                    }
                    else
                    {
                        currentUserShare = new ShareDTO
                        {
                            OwnerId = currentUser.Id,
                            Amount  = buyOffer.Amount,
                            StockId = buyOffer.StockId,
                        };
                        _shareService.AddShare(currentUserShare, statistics);
                    }

                    //Modify offer that we took shares from
                    _sellOfferService.Edit(offer, statistics);
                    buyOffer.Amount = 0;
                }
                else
                {
                    var price = offer.Amount * offer.Price;

                    buyOffer.Amount -= offer.Amount;
                    tradedAmount     = offer.Amount;
                    var targetUser = _userService.GetUserById(offer.SellerId, statistics);

                    //money got exchanged
                    targetUser.Value    += price;
                    currentUser.Value   -= price;
                    currentUser.Password = null;
                    targetUser.Password  = null;
                    _userService.EditUser(currentUser.Id, currentUser, statistics);
                    _userService.EditUser(targetUser.Id, targetUser, statistics);

                    //taking shares from seller offer
                    var targetedShare = _shareService.GetShareById(offer.ShareId, statistics);
                    targetedShare.Amount -= offer.Amount;
                    _shareService.EditShare(targetedShare.Id, targetedShare, statistics);

                    //giving shares to buyer
                    var currentUserShare = _shareService.GetAllShares(statistics).Where(c => c.OwnerId == currentUser.Id).Where(c => c.StockId == buyOffer.StockId).FirstOrDefault();
                    if (currentUserShare != null)
                    {
                        currentUserShare.Amount += buyOffer.Amount;
                        _shareService.EditShare(currentUserShare.Id, currentUserShare, statistics);
                    }
                    else
                    {
                        currentUserShare = new ShareDTO
                        {
                            OwnerId = currentUser.Id,
                            Amount  = buyOffer.Amount,
                            StockId = buyOffer.StockId,
                        };
                        _shareService.AddShare(currentUserShare, statistics);
                    }

                    //deleting offer
                    _sellOfferService.Delete(offer.Id, statistics);
                }

                TransactionDTO transaction = new TransactionDTO
                {
                    Amount   = tradedAmount,
                    Price    = offer.Price,
                    BuyerId  = currentUser.Id,
                    SellerId = offer.SellerId,
                    StockId  = buyOffer.StockId,
                    Date     = DateTime.Now
                };
                _transactionService.Add(transaction, statistics);
            }

            if (buyOffer.Amount > 0)
            {
                buyOffer.BuyerId = currentUser.Id;
                _buyOfferService.Add(buyOffer, statistics);
                //freeze users money equivalent to amount of shares he wants to buy left after searching through the market
                currentUser.Value   -= buyOffer.Amount * buyOffer.Price;
                currentUser.Password = null;
                _userService.EditUser(currentUser.Id, currentUser, statistics);
            }

            CalculatePriceChange(buyOffer.StockId, statistics);
        }
Ejemplo n.º 7
0
        public void FindBuyOffers(SellOfferDTO sellOffer, UserDTO currentUser, StatisticsDTO statistics)
        {
            //get all buy offers for stock specified in offer
            var share = _shareService.GetShareById(sellOffer.ShareId, statistics);

            if (share.Amount < sellOffer.Amount)
            {
                return;
            }
            var offers = _buyOfferService.GetAll(statistics)
                         .Where(o => o.StockId == share.StockId)  //check if stock matched
                         .Where(o => o.Price >= sellOffer.Price)  //price greater or equeal one in created offer
                         .Where(o => o.BuyerId != currentUser.Id) //remove current user offers
                         .OrderByDescending(o => o.Price);        //order by most expensive - shares goes to a highest bidder

            foreach (var offer in offers)
            {
                int tradedAmount = 0;

                //after this case offer is empty and we don't add it to database
                if (sellOffer.Amount < offer.Amount)
                {
                    //sold some amount of shares to some user and subtract amount from this users offer
                    offer.Amount -= sellOffer.Amount;
                    _buyOfferService.Edit(offer, statistics);

                    //subtract those sold shares from current user share entry
                    share.Amount -= sellOffer.Amount;
                    _shareService.EditShare(share.Id, share, statistics);

                    //add sold shares to buyer share or create if doesn't have one
                    var buyerShare = _shareService.GetAllShares(statistics).Where(c => c.OwnerId == offer.BuyerId).FirstOrDefault();
                    if (buyerShare != null)
                    {
                        buyerShare.Amount += sellOffer.Amount;
                        _shareService.EditShare(buyerShare.Id, buyerShare, statistics);
                    }
                    else
                    {
                        buyerShare = new ShareDTO
                        {
                            OwnerId = currentUser.Id,
                            Amount  = sellOffer.Amount,
                            StockId = offer.StockId,
                        };
                        _shareService.AddShare(buyerShare, statistics);
                    }

                    //give money to current user(share holder)
                    currentUser.Value   += sellOffer.Amount * sellOffer.Price;
                    currentUser.Password = null;
                    _userService.EditUser(currentUser.Id, currentUser, statistics);

                    tradedAmount     = sellOffer.Amount;
                    sellOffer.Amount = 0;
                }
                //this case implies that amount in created offer is greater than selected offer
                else
                {
                    sellOffer.Amount -= offer.Amount;

                    share.Amount -= offer.Amount;
                    _shareService.EditShare(share.Id, share, statistics);

                    currentUser.Value   += offer.Amount * offer.Price;
                    currentUser.Password = null;
                    _userService.EditUser(currentUser.Id, currentUser, statistics);


                    _buyOfferService.Delete(offer.Id, statistics);
                }

                //adding transaction for each iteration of trading
                TransactionDTO transaction = new TransactionDTO
                {
                    Amount   = tradedAmount,
                    Price    = offer.Price,
                    BuyerId  = offer.BuyerId,
                    SellerId = currentUser.Id,
                    StockId  = offer.StockId,
                    Date     = DateTime.Now
                };
                _transactionService.Add(transaction, statistics);
            }

            if (sellOffer.Amount > 0)
            {
                sellOffer.SellerId = currentUser.Id;
                _sellOfferService.Add(sellOffer, statistics);
                //freeze currentuser shares equivalent to amount left in offer
                share.Amount -= sellOffer.Amount;
                _shareService.EditShare(share.Id, share, statistics);
            }

            CalculatePriceChange(share.StockId, statistics);
        }