Exemplo n.º 1
0
        public async Task SellOwning(Guid userId, OwningDto owning)
        {
            var profile = await _repo.GetByUserId(userId);

            SellFromOwning(profile, owning);
            await _repo.Update(profile.Id, profile);
        }
Exemplo n.º 2
0
        public void Should_Match_Remaining_Quantity(List <int> ownings, int soldQuantity, int expected)
        {
            var profile = new Profile();

            foreach (var owning in ownings)
            {
                profile.Ownings.Add(new Owning
                {
                    Name             = StockName,
                    PurchaseQuantity = owning,
                    PurchaseDate     = DateTime.UtcNow + new TimeSpan(owning, 0, 0, 0),
                    PurchaseValue    = 10 + owning
                });
            }
            var       service    = new ProfileService(null);
            OwningDto soldOwning = new OwningDto()
            {
                Name             = StockName,
                PurchaseQuantity = soldQuantity,
                SellValue        = 45,
                SellDate         = DateTime.UtcNow
            };

            service.SellFromOwning(profile, soldOwning);
            Assert.Equal(expected, profile.Ownings.Where(x => x.Name == soldOwning.Name).Sum(y => y.PurchaseQuantity));
        }
Exemplo n.º 3
0
        public IActionResult InsertOwning()
        {
            var dto = new OwningDto {
                PurchaseDate = DateTime.UtcNow.Date
            };

            return(View(dto));
        }
Exemplo n.º 4
0
        public async Task <Profile> InsertOwning(Guid userId, OwningDto owning)
        {
            var profile = await _repo.GetByUserId(userId);

            profile.Ownings.Add(owning.Convert());
            await _repo.Update(profile.Id, profile);

            return(profile);
        }
Exemplo n.º 5
0
        public async Task <IActionResult> SellOwning([FromQuery] string name, [FromQuery] int quantity)
        {
            var stock = await StockContext.GetByName(name);

            var dto = new OwningDto
            {
                PurchaseQuantity = quantity,
                Name             = name,
                SellDate         = DateTime.UtcNow,
                CurrentValue     = stock?.FinalPrice
            };

            return(View(dto));
        }
Exemplo n.º 6
0
        public void Should_throw_error_above_quantity()
        {
            int       soldQuantity = 12;
            var       profile      = profileWith_8_2;
            var       service      = new ProfileService(null);
            OwningDto soldOwning   = new OwningDto()
            {
                Name             = StockName,
                PurchaseQuantity = soldQuantity,
                SellValue        = 45,
                SellDate         = DateTime.UtcNow
            };

            Assert.Throws <Exception>(() => service.SellFromOwning(profile, soldOwning));
        }
Exemplo n.º 7
0
        public void SellFromOwning(Profile profile, OwningDto soldOwning)
        {
            bool isFirst          = true;
            int  totalOwningCount = 0;
            var  ownings          = profile.Ownings.Where(x => x.Name == soldOwning.Name).OrderBy(x => x.PurchaseDate).ToList();

            ownings.ForEach(x => totalOwningCount += x.PurchaseQuantity);
            if (totalOwningCount < soldOwning.PurchaseQuantity)
            {
                throw new Exception("Sahip olunandan daha fazla satış yapılamaz");
            }
            var    soldOwnings         = new List <Owning>();
            Owning remainingOwning     = null;
            var    remainingOwningList = new List <Owning>();
            int    remainingQuantity   = soldOwning.PurchaseQuantity;

            foreach (var owning in ownings)
            {
                if (owning.PurchaseQuantity <= remainingQuantity)
                {
                    owning.SellDate    = soldOwning.SellDate;
                    owning.SellValue   = soldOwning.SellValue;
                    remainingQuantity -= owning.PurchaseQuantity;
                    soldOwnings.Add(owning);
                }
                else
                {
                    if (isFirst)
                    {
                        remainingOwning = (Owning)owning.Clone();
                        remainingOwning.PurchaseQuantity -= remainingQuantity;
                        remainingOwningList.Add(remainingOwning);
                        isFirst = false;
                    }
                    else
                    {
                        remainingOwningList.Add(owning);
                    }
                }
            }

            profile.Ownings.RemoveAll(x => x.Name == soldOwning.Name);
            if (remainingOwningList.Count > 0)
            {
                profile.Ownings.AddRange(remainingOwningList);
            }
            profile.Solds.AddRange(soldOwnings);
        }
Exemplo n.º 8
0
        public void AllSold_Solds_Count_Should_Be_2()
        {
            int       soldQuantity = 10;
            var       profile      = profileWith_8_2;
            var       service      = new ProfileService(null);
            OwningDto soldOwning   = new OwningDto()
            {
                Name             = StockName,
                PurchaseQuantity = soldQuantity,
                SellValue        = 45,
                SellDate         = DateTime.UtcNow
            };

            service.SellFromOwning(profile, soldOwning);
            Assert.Equal(2, profile.Solds.Count);
        }
Exemplo n.º 9
0
 public static Owning Convert(this OwningDto owning)
 {
     return(new Owning
     {
         Name = owning.Name,
         SellCommission = owning.SellCommission,
         SellDate = owning.SellDate,
         PurchaseDate = owning.PurchaseDate,
         //SellQuantity = owning.SellQuantity,
         SellValue = owning.SellValue,
         //sIsSold = owning.IsSold,
         PurchaseCommission = owning.PurchaseCommission,
         PurchaseQuantity = owning.PurchaseQuantity,
         PurchaseValue = owning.PurchaseValue
     });
 }
Exemplo n.º 10
0
        public async Task <IActionResult> InsertOwning(OwningDto owning)
        {
            var profile = await _service.InsertOwning(userId, owning);

            return(View());
        }
Exemplo n.º 11
0
        public async Task <IActionResult> SellOwning(OwningDto owning)
        {
            await _service.SellOwning(userId, owning);

            return(View());
        }