示例#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);
        }
示例#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));
        }
        public IActionResult InsertOwning()
        {
            var dto = new OwningDto {
                PurchaseDate = DateTime.UtcNow.Date
            };

            return(View(dto));
        }
示例#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);
        }
        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));
        }
示例#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));
        }
示例#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);
        }
示例#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);
        }
示例#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
     });
 }
        public async Task <IActionResult> InsertOwning(OwningDto owning)
        {
            var profile = await _service.InsertOwning(userId, owning);

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

            return(View());
        }