public async Task <ActionResult> DeleteTrade(TradeRequestViewModel model)
        {
            Agency db       = new Agency();
            int    deleteId = model.EstateObjectId;
            await tradeService.DeleteTrade(deleteId);

            return(RedirectToAction("Trades"));
        }
        public async Task <ActionResult> DeleteTrade(int id)
        {
            Agency       db = new Agency();
            EstateObject eo = await db.EstateObjects.FirstOrDefaultAsync(f => f.Id == id);

            TradeRequestViewModel model = await tradeService.Model(eo);

            return(View(model));
        }
        public async Task <ActionResult> UpdateTrade(TradeRequestViewModel model)
        {
            int estateObjectId    = model.EstateObjectId;
            int paymentInstrument = model.PaymentInstrumentId;
            int paymentType       = model.PaymentTypeId;
            await _tradeService.UpdateTrade(estateObjectId, paymentType, paymentInstrument);

            return(RedirectToAction("Trades"));
        }
Beispiel #4
0
        //A method to start a TradeRequest, called when a user first clicks a "Request Trade" button
        public async Task <IActionResult> StartRequest(int id)
        {
            //Creates a ViewModel so our data can persists when we switch to a new page
            TradeRequestViewModel model       = new TradeRequestViewModel();
            ApplicationUser       currentUser = await GetCurrentUserAsync();

            //Gets all the info of the Pokemon the user wishes to request
            model.DesiredPokemon = await _context.CaughtPokemon
                                   .Include(cp => cp.Pokemon)
                                   .Include(cp => cp.Gender)
                                   .Include(cp => cp.User)
                                   .Where(cp => cp.User.Id != currentUser.Id)
                                   .Where(cp => cp.isOwned == true)
                                   .Where(cp => cp.isHidden == false)
                                   .Where(cp => cp.isTradeOpen == true)
                                   .FirstOrDefaultAsync(cp => cp.Id == id);

            //If that Pokemon doesn't exist, return null
            if (model.DesiredPokemon == null)
            {
                return(NotFound());
            }

            //Sets the ID of this Pokemon to our ViewModel
            model.DesiredPokemonId = id;

            //Checks to see if the user owns a Pokemon of the same species as the desired Pokemon
            CaughtPokemon DesiredPokemonInUserCollection = await _context.CaughtPokemon
                                                           .Include(cp => cp.Pokemon)
                                                           .Include(cp => cp.Gender)
                                                           .Include(cp => cp.User)
                                                           .Where(cp => cp.User.Id == currentUser.Id)
                                                           .Where(cp => cp.isOwned == true)
                                                           .Where(cp => cp.isHidden == false)
                                                           .FirstOrDefaultAsync(cp => cp.PokemonId == model.DesiredPokemon.PokemonId);

            //If they do, we'll set a boolean to true, so that the Pokemon is highlighted as Caught
            if (DesiredPokemonInUserCollection != null)
            {
                model.isDesiredOwned = true;
            }

            //Fetches a list of all Pokemon the currently logged in user owns AND is marked for Trade. Users will be able to select a Pokemon to offer up in exchange for the Pokemon they're requesting
            model.TradeOpenPokemon = await _context.CaughtPokemon
                                     .Include(cp => cp.Pokemon)
                                     .Include(cp => cp.Gender)
                                     .Include(cp => cp.User)
                                     .Where(cp => cp.User.Id == currentUser.Id)
                                     .Where(cp => cp.isOwned == true)
                                     .Where(cp => cp.isHidden == false)
                                     .Where(cp => cp.isTradeOpen == true)
                                     .ToListAsync();

            //Sends our completed ViewModel to our View
            return(View(model));
        }
Beispiel #5
0
        public IActionResult StartRequest(TradeRequestViewModel model)
        {
            //Creates a new ViewModel for the two Pokemon...
            FinalTradeRequestViewModel passedModel = new FinalTradeRequestViewModel()
            {
                DesiredPokemonId = model.DesiredPokemonId,
                OfferedPokemonId = model.OfferedPokemonId,
                isDesiredOwned   = model.isDesiredOwned
            };

            //...and sends it to our FinalizeRequest method, which will open a new page ofr the user to finalize their request
            return(RedirectToAction("FinalizeRequest", passedModel));
        }
        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);
            }
        }
Beispiel #7
0
        //Runs when the user is bout to finalize their TradeRequest. Fetches both Pokemon associated, and asks the user to add a comment to their request
        public async Task <IActionResult> FinalizeRequest(FinalTradeRequestViewModel passedModel)
        {
            TradeRequestViewModel model = new TradeRequestViewModel()
            {
                DesiredPokemonId = passedModel.DesiredPokemonId,
                OfferedPokemonId = passedModel.OfferedPokemonId,
                isDesiredOwned   = passedModel.isDesiredOwned
            };

            ApplicationUser currentUser = await GetCurrentUserAsync();

            //Fetches the Pokemon the user is requesting
            model.DesiredPokemon = await _context.CaughtPokemon
                                   .Include(cp => cp.Pokemon)
                                   .Include(cp => cp.Gender)
                                   .Include(cp => cp.User)
                                   .Where(cp => cp.User.Id != currentUser.Id)
                                   .Where(cp => cp.isOwned == true)
                                   .Where(cp => cp.isHidden == false)
                                   .Where(cp => cp.isTradeOpen == true)
                                   .FirstOrDefaultAsync(cp => cp.Id == passedModel.DesiredPokemonId);

            if (model.DesiredPokemon == null)
            {
                return(NotFound());
            }

            //Fetches the Pokemon the user is offering in return
            model.OfferedPokemon = await _context.CaughtPokemon
                                   .Include(cp => cp.Pokemon)
                                   .Include(cp => cp.Gender)
                                   .Include(cp => cp.User)
                                   .Where(cp => cp.User.Id == currentUser.Id)
                                   .Where(cp => cp.isOwned == true)
                                   .Where(cp => cp.isHidden == false)
                                   .Where(cp => cp.isTradeOpen == true)
                                   .FirstOrDefaultAsync(cp => cp.Id == passedModel.OfferedPokemonId);

            if (model.OfferedPokemon == null)
            {
                return(NotFound());
            }

            //Returns these two Pokemon to our View
            return(View(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);
            }
        }
Beispiel #10
0
        public async Task <IActionResult> FinalizeRequest(TradeRequestViewModel passedModel)
        {
            //Removes some unneccesary fields from our ModelState
            ModelState.Remove("DesiredPokemon");
            ModelState.Remove("DesiredPokemon.Level");
            ModelState.Remove("OfferedPokemon");
            ModelState.Remove("TradeOpenPokemon");

            //If our ModelState is valid, we'll add this TradeRequest to our database
            if (ModelState.IsValid)
            {
                TradeRequest newTradeRequest = new TradeRequest();

                //Adds all of our IDs and comments to a new TradeRequest
                newTradeRequest.DesiredPokemonId = passedModel.DesiredPokemonId;
                newTradeRequest.OfferedPokemonId = passedModel.OfferedPokemonId;
                newTradeRequest.Comment          = passedModel.Comment.Replace("'", "''");
                newTradeRequest.isOpen           = true;

                //Checks to see if a request already exists that holds these two exact Pokemon
                TradeRequest potentialDuplicateRequest = await _context.TradeRequest
                                                         .Where(tr => (tr.DesiredPokemonId == newTradeRequest.DesiredPokemonId && tr.OfferedPokemonId == newTradeRequest.OfferedPokemonId) || (tr.DesiredPokemonId == newTradeRequest.OfferedPokemonId && tr.OfferedPokemonId == newTradeRequest.DesiredPokemonId))
                                                         .Where(tr => tr.isOpen == true)
                                                         .FirstOrDefaultAsync();

                //If this isn't a duplicate request, add this TradeRequest to our database
                //Important to note, this MUST be done as a SQL query for now. Using ApplicationDbContext simply doesn't work for some unknown reason, so I had to do a manual workaround for this feature of the app.
                if (potentialDuplicateRequest == null)
                {
                    string sqlCommand = $"INSERT INTO TradeRequest (DesiredPokemonId, OfferedPokemonId, Comment, isOpen, DateSent, DateCompleted) VALUES ({newTradeRequest.DesiredPokemonId}, {newTradeRequest.OfferedPokemonId}, '{newTradeRequest.Comment}', 1, '{DateTime.Now}', NULL)";
                    await _context.Database.ExecuteSqlCommandAsync(sqlCommand);
                }

                return(RedirectToAction("Index"));
            }

            //If the ModelState is invalid, we'll reload the page with the original information

            TradeRequestViewModel failedModel = new TradeRequestViewModel()
            {
                DesiredPokemonId = passedModel.DesiredPokemonId,
                OfferedPokemonId = passedModel.OfferedPokemonId,
                isDesiredOwned   = passedModel.isDesiredOwned
            };

            ApplicationUser currentUser = await GetCurrentUserAsync();



            failedModel.DesiredPokemon = await _context.CaughtPokemon
                                         .Include(cp => cp.Pokemon)
                                         .Include(cp => cp.Gender)
                                         .Include(cp => cp.User)
                                         .Where(cp => cp.User.Id != currentUser.Id)
                                         .Where(cp => cp.isOwned == true)
                                         .Where(cp => cp.isHidden == false)
                                         .Where(cp => cp.isTradeOpen == true)
                                         .FirstOrDefaultAsync(cp => cp.Id == passedModel.DesiredPokemonId);

            if (failedModel.DesiredPokemon == null)
            {
                return(NotFound());
            }

            failedModel.OfferedPokemon = await _context.CaughtPokemon
                                         .Include(cp => cp.Pokemon)
                                         .Include(cp => cp.Gender)
                                         .Include(cp => cp.User)
                                         .Where(cp => cp.User.Id == currentUser.Id)
                                         .Where(cp => cp.isOwned == true)
                                         .Where(cp => cp.isHidden == false)
                                         .Where(cp => cp.isTradeOpen == true)
                                         .FirstOrDefaultAsync(cp => cp.Id == passedModel.OfferedPokemonId);

            if (failedModel.OfferedPokemon == null)
            {
                return(NotFound());
            }

            return(View(failedModel));
        }