public void UpdateLot(LotEntity lot, bool contentChanged)
        {
            using (var unitOfWork = new AuctionUnitOfWork())
            {
                var dbLot = unitOfWork.LotRepository.Get(lot.Id);

                if (contentChanged)
                {
                    var dbContents = unitOfWork.LotContentRepository.Get(c => c.LotId == lot.Id);

                    foreach (var content in dbContents)
                    {
                        unitOfWork.LotContentRepository.Delete(content);
                    }

                    var cnt = lot.LotContents.Select(c => new LotContent {
                        Content = c.Content, LotId = c.LotId
                    }).ToList();
                    dbLot.LotContents = cnt;
                }

                dbLot.Title       = lot.Title;
                dbLot.Description = lot.Description;
                dbLot.CategoryId  = lot.CategoryId;

                unitOfWork.Commit();
            }
        }
 public void AddLot(LotEntity lot)
 {
     using (var unitOfWork = new AuctionUnitOfWork())
     {
         unitOfWork.LotRepository.Add(lot);
         unitOfWork.Commit();
     }
 }
        private NotificationEntity BuildUserNotification(int userId, LotEntity lot)
        {
            var toUser = BuildLotNotification();

            toUser.RecieverId = userId;
            toUser.EntityId   = lot.Id;
            toUser.Message    = $"Торги по лоту \"{lot.Title}\" завершились. К сожалению, ваша ставка не была максимальной";

            return(toUser);
        }
        private NotificationEntity BuildOwnerNotification(LotEntity lot)
        {
            var toOwner = BuildLotNotification();

            toOwner.RecieverId = lot.OwnerId;
            toOwner.EntityId   = lot.Id;
            toOwner.Message    = $"Созданный вами аукцион \"{lot.Title}\" закрыт";

            return(toOwner);
        }
        private NotificationEntity BuildWinnerNotification(int userId, LotEntity lot)
        {
            var toWinner = BuildLotNotification();

            toWinner.RecieverId = userId;
            toWinner.EntityId   = lot.Id;
            toWinner.Message    = $"Торги по лоту \"{lot.Title}\" завершились. Ваша ставка была максимальной. Свяжитесь с продавцом для покупки товара.";

            return(toWinner);
        }
        /// <summary>
        /// заполнение формы редактирования лота
        /// </summary>
        /// <param name="lot"></param>
        public override void LoadLot(LotEntity lot)
        {
            _lot             = lot;
            _initialCategory = Categories.FirstOrDefault(c => c.Id == _lot.CategoryId);

            LotId            = _lot.Id;
            Title            = _lot.Title;
            SelectedCategory = _initialCategory;
            Photos           = new ObservableCollection <LotContent>(_lot.LotContents);
            Description      = _lot.Description;
            StartBid         = _lot.StartBid.ToString("0.00");

            int daysCount = (int)(_lot.DateToExpire - _lot.DateCreated).TotalDays;

            SelectedDaysCount = DaysCount.FirstOrDefault(l => l.DaysCount == daysCount);
        }
        public void Initialize(int lotId)
        {
            _lot = _lotService.GetLot(lotId);

            if (_lot != null)
            {
                _owner       = _lot.Owner;
                Title        = _lot.Title;
                Description  = _lot.Description;
                CategoryName = _lot.Category.Name;
                CurrentBid   = _lot.CurrentBid;
                ExpireDate   = _lot.DateToExpire;
                BidsCount    = _lot.Bids.Count;
                _bids        = new ObservableCollection <Bid>(_lot.Bids.ToList());

                LoadContent(_lot);

                double daysToExpire = (_lot.DateToExpire - DateTime.Now).TotalDays;

                if (daysToExpire < 0)
                {
                    _isExpired = true;
                    NotifyExpired();
                    DaysToExpire = 0;
                }
                else
                {
                    DaysToExpire = (int)Math.Ceiling(daysToExpire);
                }

                if (_session.IsLoggedIn)
                {
                    CurrentUserIsOwner = _owner.Id == _session.User.Id;
                }

                IsLotLoaded = true;

                if (!IsActive)
                {
                    DaysToExpire = 0;
                    Title       += " (не активен)";
                }
            }

            OnPropertyChanged(nameof(IsLotLoaded));
            OnPropertyChanged(nameof(LotIsNotLoaded));
        }
        /// <summary>
        /// оповещает каждого пользователя, участвующего в торгах по лоту, о его завершении
        /// </summary>
        /// <param name="unitOfWork"></param>
        /// <param name="lot"></param>
        private void Notify(AuctionUnitOfWork unitOfWork, LotEntity lot)
        {
            var toOwner = BuildOwnerNotification(lot);

            _notificationService.Send(toOwner, unitOfWork);

            if (lot.Bids == null || !lot.Bids.Any())
            {
                return;
            }

            var maxBid = lot.Bids.OrderByDescending(b => b.Amount).FirstOrDefault();
            var winner = maxBid.User;

            var toWinner = BuildWinnerNotification(maxBid.UserId, lot);

            _notificationService.Send(toWinner, unitOfWork);

            foreach (var user in lot.Users.Where(u => u.Id != winner.Id))
            {
                var toUser = BuildUserNotification(user.Id, lot);
                _notificationService.Send(toUser, unitOfWork);
            }
        }
        protected override void HandleLotAction(LotEntity lot)
        {
            LotService.UpdateLot(lot, HasContentChanged);

            View.OnLotAction(lot, FormMode.Update);
        }