public async Task <MessageViewModel> CreateTrade(int estateObjectId, int managerId)
        {
            using (Agency db = new Agency())
            {
                MessageViewModel model   = new MessageViewModel();
                Request          request = await db.Requests.FirstOrDefaultAsync(f => f.EstateObjectId == estateObjectId);

                EstateObject eo = await db.EstateObjects.FirstOrDefaultAsync(f => f.Id == estateObjectId);

                if (eo.StatusId == 2)
                {
                    eo.StatusId = 3;
                    Trade trade = new Trade
                    {
                        EstateObjectId = request.EstateObjectId,
                        ClientId       = request.ClientId,
                        ManagerId      = managerId,
                        Date           = DateTime.Now
                    };
                    db.Trades.Add(trade);
                    request.TradeId = trade.Id;
                    await db.SaveChangesAsync();
                }
                else
                {
                    model.Type    = "Ошибка";
                    model.Message = "Очевидно, другой менеджер только что рассмотрел эту заявку, или её отменили.";
                }
                return(model);
            }
        }
        public async Task <MessageViewModel> CreateRequest(int clientId, int estateObjectId)
        {
            using (Agency db = new Agency())
            {
                MessageViewModel message = new MessageViewModel();

                EstateObject eo = await db.EstateObjects.FirstOrDefaultAsync(f => f.Id == estateObjectId);

                if (eo.StatusId == 1)
                {
                    eo.StatusId = 2;
                    Request request = new Request {
                        EstateObjectId = estateObjectId, ClientId = clientId
                    };
                    db.Requests.Add(request);
                    await db.SaveChangesAsync();

                    message.Type    = "Заявка подана на рассмотрение!";
                    message.Message = "Обновите страницу Активные заявки";
                }
                else
                {
                    message.Type    = "Ошибка";
                    message.Message = "К сожалению, кто-то только что купил этот объект недвижимости. Рекомендуем выбрать другой вариант.";
                }
                return(message);
            }
        }
        public async Task <EstateObjectViewModel> Model(EstateObject eo)
        {
            List <Picture> pictures = await EstateObjectsRepository.Pictures(eo.Id);

            EstateObjectViewModel model = new EstateObjectViewModel
            {
                Id              = eo.Id,
                Price           = eo.Price,
                Address         = eo.Address,
                Description     = eo.Description,
                Area            = eo.Area,
                Rooms           = eo.Rooms,
                LandDescription = eo.LandDescription,
                LandArea        = eo.LandArea,
                District        = eo.District.Name,
                RealtyType      = eo.RealtyType.Name,
                TradeType       = eo.TradeType.Name,
                Owner           = eo.Owner.Phone,
                Pictures        = pictures,
                RealtyTypeId    = eo.RealtyTypeId,
                TradeTypeId     = eo.TradeTypeId,
                DistrictId      = eo.DistrictId,
                OwnerId         = eo.OwnerId,
                RealtyTypes     = PrepareRealtyTypes(),
                TradeTypes      = PrepareTradeTypes(),
                Districts       = PrepareDistricts(),
                Owners          = PrepareOwners(),
            };

            return(model);
        }
 public async Task <EstateObject> UpdateEstateObject(EstateObject estateObject)
 {
     using (Agency db = new Agency())
     {
         db.Entry(estateObject).State = EntityState.Modified;
         await db.SaveChangesAsync();
     }
     return(estateObject);
 }
        public async Task <EstateObject> GetEstateObject(int id)
        {
            EstateObject result = null;

            using (Agency db = new Agency())
            {
                result = await db.EstateObjects.FirstOrDefaultAsync(f => f.Id == id);
            }
            return(result);
        }
        public async Task <EstateObject> AddEstateObject(EstateObject estateObject)
        {
            EstateObject result = null;

            using (Agency db = new Agency())
            {
                result = db.EstateObjects.Add(estateObject);
                await db.SaveChangesAsync();
            }
            return(result);
        }
        public async Task DeleteRequest(int estateObjectId)
        {
            using (var db = new Agency())
            {
                Request result = await db.Requests.FirstOrDefaultAsync(f => f.EstateObjectId == estateObjectId);

                EstateObject eo = await db.EstateObjects.FirstOrDefaultAsync(f => f.Id == estateObjectId);

                eo.StatusId = 1;
                db.Requests.Remove(result);
                await db.SaveChangesAsync();
            }
        }
        public async Task <List <EstateObjectViewModel> > GetEstateObjectViewModels(List <EstateObject> estateObjects)
        {
            using (Agency db = new Agency())
            {
                List <EstateObjectViewModel> list = new List <EstateObjectViewModel>();
                foreach (var ob in estateObjects)
                {
                    EstateObject eo = await db.EstateObjects.FirstOrDefaultAsync(f => f.Id == ob.Id);

                    list.Add(await Model(eo));
                }
                return(list);
            }
        }
        public async Task DeleteTrade(int estateObjectId)
        {
            using (Agency db = new Agency())
            {
                EstateObject eo = await db.EstateObjects.FirstOrDefaultAsync(f => f.Id == estateObjectId);

                eo.StatusId = 1;

                Trade trade = await db.Trades.FirstOrDefaultAsync(f => f.EstateObjectId == estateObjectId);

                db.Trades.Remove(trade);

                await db.SaveChangesAsync();
            }
        }
        public async Task <TradeRequestViewModel> Model(EstateObject eo)
        {
            using (Agency db = new Agency())
            {
                Trade trade = await db.Trades.FirstOrDefaultAsync(f => f.EstateObjectId == eo.Id);

                TradeRequestViewModel model = new TradeRequestViewModel();
                model = new TradeRequestViewModel
                {
                    EstateObjectId = eo.Id,
                    ClientId       = trade.ClientId,
                    ManagerId      = trade.ManagerId,
                    TradeId        = Convert.ToInt32(trade.Id),

                    Price           = eo.Price,
                    Address         = eo.Address,
                    Description     = eo.Description,
                    Area            = eo.Area,
                    Rooms           = eo.Rooms,
                    LandDescription = eo.LandDescription,
                    LandArea        = eo.LandArea,
                    District        = eo.District.Name,
                    RealtyType      = eo.RealtyType.Name,
                    TradeType       = eo.TradeType.Name,
                    Owner           = eo.Owner.Surname + " " + eo.Owner.Name + " " + eo.Owner.Patronymic + ": " + eo.Owner.Phone,

                    ClientEmail = trade.Client.Email,
                    ClientName  = trade.Client.Surname + " " + trade.Client.Name + " " + trade.Client.Patronymic,
                    ClientPhone = trade.Client.Phone,

                    ManagerEmail = trade.Manager.Email,
                    ManagerName  = trade.Manager.Surname + " " + trade.Manager.Name + " " + trade.Manager.Patronymic,
                    ManagerPhone = trade.Manager.Phone,

                    Date = trade.Date,
                    PaymentInstruments = PreparePaymentInstruments(),
                    PaymentTypes       = PreparePaymentTypes(),

                    RealtyTypeId = eo.RealtyTypeId
                };
                if (trade.PaymentInstrument != null)
                {
                    model.PaymentInstrument = trade.PaymentInstrument.Name;
                    model.PaymentType       = trade.PaymentType.Name;
                }
                return(model);
            }
        }
        public async Task <List <TradeRequestViewModel> > GetTrades(DateTime firstDate, DateTime secondDate)
        {
            using (Agency db = new Agency())
            {
                var trades = from Trade in db.Trades
                             where Trade.Date >= firstDate && Trade.Date <= secondDate
                             select Trade;
                var myTrades = new List <TradeRequestViewModel>();
                foreach (Trade trade in trades)
                {
                    EstateObject eo = await db.EstateObjects.FirstOrDefaultAsync(f => f.Id == trade.EstateObjectId);

                    TradeRequestViewModel model = await Model(eo);

                    myTrades.Add(model);
                }
                return(myTrades);
            }
        }
        public async Task <List <TradeRequestViewModel> > AllTrades()
        {
            using (Agency db = new Agency())
            {
                var trades = from Trade in db.Trades
                             select Trade;
                var myTrades = new List <TradeRequestViewModel>();
                foreach (Trade trade in trades)
                {
                    EstateObject eo = await db.EstateObjects.FirstOrDefaultAsync(f => f.Id == trade.EstateObjectId);

                    TradeRequestViewModel model = await Model(eo);

                    myTrades.Add(model);
                }
                myTrades.Reverse();
                return(myTrades);
            }
        }
        public async Task UpdateTrade(int estateObjectId, int paymentTypeId, int paymentInstrument)
        {
            using (var db = new Agency())
            {
                Request request = await db.Requests.FirstOrDefaultAsync(f => f.EstateObjectId == estateObjectId);

                EstateObject eo = await db.EstateObjects.FirstOrDefaultAsync(f => f.Id == estateObjectId);

                eo.StatusId = 4;

                int   tradeId = Convert.ToInt32(request.TradeId);
                Trade trade   = await db.Trades.FirstOrDefaultAsync(f => f.Id == tradeId);

                trade.PaymentTypeId       = paymentTypeId;
                trade.PaymentInstrumentId = paymentInstrument;

                db.Requests.Remove(request);
                await db.SaveChangesAsync();
            }
        }
        public async Task <EstateObject> EstateObject(EstateObjectViewModel model)
        {
            List <Picture> pictures = await EstateObjectsRepository.Pictures(model.Id);

            EstateObject eo = new EstateObject
            {
                Id              = model.Id,
                Price           = model.Price,
                Address         = model.Address,
                Description     = model.Description,
                Area            = model.Area,
                Rooms           = model.Rooms,
                LandDescription = model.LandDescription,
                LandArea        = model.LandArea,
                RealtyTypeId    = model.RealtyTypeId,
                TradeTypeId     = model.TradeTypeId,
                DistrictId      = model.DistrictId,
                OwnerId         = model.OwnerId,
                StatusId        = 1,
            };

            return(eo);
        }
        public async Task <MessageViewModel> RejectRequest(int id)
        {
            using (Agency db = new Agency())
            {
                MessageViewModel message = new MessageViewModel();
                EstateObject     eo      = await db.EstateObjects.FirstOrDefaultAsync(f => f.Id == id);

                if (eo.StatusId == 2)
                {
                    eo.StatusId = 1;
                    Request req = await db.Requests.FirstOrDefaultAsync(f => f.EstateObjectId == id);

                    db.Requests.Remove(req);
                    await db.SaveChangesAsync();
                }
                else
                {
                    message.Type    = "Ошибка";
                    message.Message = "Очевидно, другой менеджер только что рассмотрел эту заявку, или её отменили.";
                }
                return(message);
            }
        }
Esempio n. 16
0
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            try
            {
                int idEstate = estateObject.idEstate;
                var ctx      = ClassGetContext.context;
                estateObject = new EstateObject
                {
                    idEstate      = idEstate,
                    city          = textBoxCity.Text,
                    street        = textBoxStreet.Text,
                    house         = textBoxHouse.Text,
                    addressNumber = textBoxApart.Text,
                    floor         = Convert.ToInt32(string.IsNullOrEmpty(textBoxFloor.Text) ? "0" : textBoxFloor.Text),
                    totalArea     = numArea.Value,
                    totalFloors   = Convert.ToInt32(numTotalFloor.Value),
                    rooms         = Convert.ToInt32(numTotalRooms.Value),
                    longitude     = numLongit.Value,
                    latitude      = numLatit.Value,
                    typeEstate    = comboBoxEstateEdit.Text,
                };

                ctx.EstateObjects.Add(estateObject);
                ctx.SaveChanges();

                using (var form = new FormMessage("Недвижимость добавлена", ChangePic.success))
                    form.ShowDialog();

                FillTable(ctx.EstateObjects.ToList());
                BoxesVision();
            }
            catch
            {
                using (var form = new FormMessage("Что-то пошло не так...", ChangePic.error))
                    form.ShowDialog();
            }
        }
 public async Task <EstateObject> UpdateEstateObject(EstateObject estateObject)
 {
     return(await _estateObjectsRepository.UpdateEstateObject(estateObject));
 }