Exemplo n.º 1
0
        public ItemFullViewDTO GetItemById(Guid id)
        {
            var similarItems = new List <ItemHeader>();

            var returnDto = new ItemFullViewDTO(this.context.Items
                                                .Include(x => x.Ratings)
                                                .Include(x => x.Comments).ThenInclude(c => c.User)
                                                .SingleOrDefault(x => x.Id == id));

            var temp = this.context.Items.Where(x => x.Category == EnumExtensionMethods.GetValueFromDescription <Category>(returnDto.Category)).Include(x => x.Ratings).Include(x => x.Comments).ToList();

            temp.ForEach(x =>
            {
                if (x.Id != id)
                {
                    similarItems.Add(new ItemHeader(x));
                }
            });

            returnDto.SimilarItems = new PagedResult <ItemHeader>
            {
                AllResultsCount = similarItems.Count <= 3 ? similarItems.Count : 3,
                Results         = similarItems.Count <= 3 ? similarItems : similarItems.Take(3).ToList(),
                PageNumber      = 0,
                PageSize        = 1
            };

            return(returnDto);
        }
Exemplo n.º 2
0
        public async Task <IActionResult> OnPostNextAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }
            var wizardEvent = HttpContext.Session.GetJson <RideEvent>("WizardEvent");

            wizardEvent.MaxSignup     = EventWizard2.MaxSignup;
            wizardEvent.MaxSignUpType = EnumExtensionMethods.GetValueFromDescription <MaxSignUpType>(Input.SelectedMaxSignUpType);
            wizardEvent.Status        = EnumExtensionMethods.GetValueFromDescription <Status>(Input.SelectedStatus);
            HttpContext.Session.SetJson("WizardEvent", null);
            _context.RideEvent.Add(wizardEvent);
            foreach (var user in Input.SelectedUser)
            {
                // finds single selected user in list by full name
                // will change this so it searches by id instead
                var selectedUser = await _context.Users
                                   .AsNoTracking()
                                   .FirstOrDefaultAsync(m => m.FullName == user);

                RideLeaderAssignment RideLeaderAssignment = new RideLeaderAssignment
                {
                    InTandemUserID = selectedUser.Id,
                    RideEventID    = wizardEvent.ID
                };
                _context.RideLeaderAssignment.Add(RideLeaderAssignment);
            }
            await _context.SaveChangesAsync();

            return(RedirectToPage("./Index"));
        }
Exemplo n.º 3
0
        public void CreateOrder(CreateOrderDTO orderDto)
        {
            var items = new List <OrderItem>();
            var user  = this.context.Users.Include(x => x.CartItems).SingleOrDefault(x => x.Id == orderDto.UserId);

            user.CartItems.ForEach(x =>
            {
                items.Add(new OrderItem()
                {
                    ItemId = x.ItemId
                });
            });

            if (orderDto.BillingAddres.Country == null)
            {
                orderDto.BillingAddres.Country = orderDto.ShippingAddres.Country;
            }

            if (orderDto.BillingAddres.ZipCode == null)
            {
                orderDto.BillingAddres.ZipCode = orderDto.ShippingAddres.ZipCode;
            }

            if (orderDto.BillingAddres.Street == null)
            {
                orderDto.BillingAddres.Street = orderDto.ShippingAddres.Street;
            }

            if (orderDto.BillingAddres.HouseNumberAndDoor == null)
            {
                orderDto.BillingAddres.HouseNumberAndDoor = orderDto.ShippingAddres.HouseNumberAndDoor;
            }

            var order = new Order()
            {
                UserId          = orderDto.UserId,
                OrderStatus     = Dal.Enums.OrderStatus.New,
                PaymentMethod   = EnumExtensionMethods.GetValueFromDescription <PaymentMethod>(orderDto.PaymentMethod),
                Items           = items,
                BillingAddress  = orderDto.BillingAddres,
                ShippingAddress = orderDto.ShippingAddres,
                OrderDate       = DateTime.Now,
                DeliveryDate    = null,
                Comment         = orderDto.Comment
            };

            this.context.Orders.Add(order);
            this.context.SaveChanges();

            order.Items.ForEach(x => x.OrderId = order.Id);
            user.CartItems.ForEach(x =>
            {
                context.UserCartItems.Remove(x);
            });
            this.context.SaveChanges();
        }
        } // OnGet

        public IActionResult OnPost()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }
            var wizardEvent = HttpContext.Session.GetJson <RideEvent>("WizardEvent");

            wizardEvent.EventName   = EventWizard1.EventName;
            wizardEvent.EventDate   = EventWizard1.EventDate;
            wizardEvent.Description = EventWizard1.Description;
            wizardEvent.Location    = EventWizard1.Location;
            wizardEvent.Distance    = EventWizard1.Distance;
            wizardEvent.EventType   = EnumExtensionMethods.GetValueFromDescription <EventType>(Input.SelectedEventType);
            HttpContext.Session.SetJson("WizardEvent", wizardEvent);


            return(RedirectToPage("./EventWizard2"));
        } //OnPost
Exemplo n.º 5
0
        public PagedResult <ItemHeader> GetAllItems(ItemSpecification specification = null)
        {
            if (specification == null)
            {
                specification = new ItemSpecification();
            }

            if (specification.PageSize < 0)
            {
                specification.PageSize = null;
            }
            if (specification.PageNumber < 0)
            {
                specification.PageNumber = null;
            }

            IQueryable <Item> query = this.context.Items
                                      .Include(x => x.Ratings);

            if (!string.IsNullOrWhiteSpace(specification.ComplexFilter))
            {
                query = query.Where(x => x.Name.Contains(specification.ComplexFilter) ||
                                    x.Manufacturer.Contains(specification.ComplexFilter) ||
                                    x.ShortDescription.Contains(specification.ComplexFilter));
            }

            if (!string.IsNullOrWhiteSpace(specification.Name))
            {
                query = query.Where(x => x.Name.Contains(specification.Name));
            }

            if (specification.SelectedCategories.Any())
            {
                var selectedCategoriesEnum = new List <Category>();
                specification.SelectedCategories.ForEach(x =>
                {
                    selectedCategoriesEnum.Add(EnumExtensionMethods.GetValueFromDescription <Category>(x));
                });
                query = query.Where(x => selectedCategoriesEnum.Contains(x.Category));
            }

            if (specification.SelectedManufacturers.Any())
            {
                query = query.Where(x => specification.SelectedManufacturers.Contains(x.Manufacturer));
            }

            if (specification.HasRGB.HasValue)
            {
                query = query.Where(x => x.HasRGB == specification.HasRGB);
            }

            if (specification.IsNewArrival.HasValue)
            {
                query = query.Where(x => x.DateSinceInStore > DateTime.Now.AddDays(-30));
            }

            if (specification.IsDiscounted.HasValue)
            {
                query = query.Where(x => x.DiscountedPrice != null && x.DiscountedPrice < x.OriginalPrice);
            }

            if (specification.IsGaming.HasValue)
            {
                query = query.Where(x => x.GamingFlag == specification.IsGaming);
            }

            if (specification.IsUsed.HasValue)
            {
                query = query.Where(x => x.IsUsed == specification.IsUsed);
            }

            if (specification.MinPrice != null)
            {
                query = query.Where(x => (x.DiscountedPrice ?? x.OriginalPrice) >= specification.MinPrice);
            }

            if (specification.MaxPrice != null)
            {
                query = query.Where(x => (x.DiscountedPrice ?? x.OriginalPrice) <= specification.MaxPrice);
            }

            if (specification.MinRating != null)
            {
                query = query.Where(x => (x.Ratings.Any() ? Convert.ToInt32(x.Ratings.Average(y => y.Value)) : 0) >= specification.MinRating);
            }

            if (specification.MaxRating != null)
            {
                query = query.Where(x => (x.Ratings.Any() ? Convert.ToInt32(x.Ratings.Average(y => y.Value)) : 0) <= specification.MaxRating);
            }

            //order
            switch (specification.Order)
            {
            case ItemSpecification.ItemOrder.PriceAscending:
                query = query.OrderBy(x => x.DiscountedPrice ?? x.OriginalPrice);
                break;

            case ItemSpecification.ItemOrder.PriceDescending:
                query = query.OrderByDescending(x => x.DiscountedPrice ?? x.OriginalPrice);
                break;

            case ItemSpecification.ItemOrder.RatingAscending:
                query = query.OrderBy(x => (x.Ratings.Any() ? Convert.ToInt32(x.Ratings.Average(y => y.Value)) : 0));
                break;

            case ItemSpecification.ItemOrder.RatingDescending:
                query = query.OrderByDescending(x => (x.Ratings.Any() ? Convert.ToInt32(x.Ratings.Average(y => y.Value)) : 0));
                break;
            }

            int?allResultsCount = null;

            if ((specification.PageSize ?? 0) != 0)
            {
                specification.PageNumber ??= 0;
                allResultsCount = query.Count();
                query           = query
                                  .Skip(specification.PageNumber.Value * specification.PageSize.Value)
                                  .Take(specification.PageSize.Value);
            }

            int totalMaxPrice, totalMinPrice;
            var maxDiscounted = context.Items.Max(x => x.DiscountedPrice);
            var maxOriginal   = context.Items.Max(x => x.OriginalPrice);
            var minDiscounted = context.Items.Min(x => x.DiscountedPrice);
            var minOriginal   = context.Items.Min(x => x.OriginalPrice);

            if (maxDiscounted != null)
            {
                totalMaxPrice = maxOriginal > maxDiscounted ? maxOriginal : maxDiscounted.Value;
                totalMinPrice = maxOriginal > maxDiscounted ? minOriginal : minDiscounted.Value;
            }
            else
            {
                totalMaxPrice = maxOriginal;
                totalMinPrice = minOriginal;
            }

            return(new PagedResult <ItemHeader>
            {
                AllResultsCount = allResultsCount,
                Results = query.ToList().Select(ItemHeaderSelectorFunc.Value),
                Categories = context.Items.Select(x => EnumExtensionMethods.GetDescription(x.Category)).Distinct().ToList(),
                Manufacturers = context.Items.Select(x => x.Manufacturer).Distinct().ToList(),
                TotalMaxPrice = totalMaxPrice,
                TotalMinPrice = totalMinPrice,
                PageNumber = specification.PageNumber,
                PageSize = specification.PageSize,
                Specification = specification,
            });
        }
Exemplo n.º 6
0
        public void CreateItem(CreateItemDTO item)
        {
            switch (EnumExtensionMethods.GetValueFromDescription <Category>(item.Category))
            {
            case Category.Cpu:
            {
                var cpu = new Cpu()
                {
                    Name             = item.Name,
                    Category         = Category.Cpu,
                    Available        = 20,
                    PicturePath      = item.PicturePath,
                    OriginalPrice    = item.OriginalPrice,
                    DiscountedPrice  = item.DiscountedPrice,
                    Manufacturer     = item.Manufacturer,
                    ShortDescription = item.ShortDescription,
                    Description      = item.Description,
                    Warranty         = item.Warranty,
                    GamingFlag       = item.GamingFlag,
                    IsUsed           = item.IsUsed,
                    HasRGB           = item.HasRGB,
                    DateSinceInStore = DateTime.Now,

                    BaseClock       = item.BaseClock.Value,
                    TDP             = item.TDP.Value,
                    ProcessorFamily = item.ProcessorFamily,
                    Technology      = item.Technology == null ? 0 : item.Technology.Value,
                    CoreNumber      = item.CoreNumber == null ? 0 : item.CoreNumber.Value,
                    ThreadNumber    = item.ThreadNumber == null ? 0 : item.ThreadNumber.Value,
                    Socket          = item.Socket
                };

                this.context.Items.Add(cpu);
                break;
            }

            case Category.Case:
            {
                var pcCase = new Case()
                {
                    Name             = item.Name,
                    Category         = Category.Cpu,
                    Available        = 20,
                    PicturePath      = item.PicturePath,
                    OriginalPrice    = item.OriginalPrice,
                    DiscountedPrice  = item.DiscountedPrice,
                    Manufacturer     = item.Manufacturer,
                    ShortDescription = item.ShortDescription,
                    Description      = item.Description,
                    Warranty         = item.Warranty,
                    GamingFlag       = item.GamingFlag,
                    IsUsed           = item.IsUsed,
                    HasRGB           = item.HasRGB,
                    DateSinceInStore = DateTime.Now,

                    BuiltInFanNumber     = item.BuiltInFanNumber == null ? 0 : item.BuiltInFanNumber.Value,
                    SupportedMotherboard = item.SupportedMotherboard,
                    Height    = item.Height == null ? 0 : item.Height.Value,
                    Width     = item.Width == null ? 0 : item.Width.Value,
                    Depth     = item.Depth == null ? 0 : item.Depth.Value,
                    HDDNumber = item.HDDNumber == null ? 0 : item.HDDNumber.Value,
                };

                this.context.Items.Add(pcCase);
                break;
            }
                //TODO: case for all types
            }
            context.SaveChanges();
        }
        } // OnGetAsync

        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }
            List <InTandemUser> SelectedInTandemUsers = new List <InTandemUser> {
            };

            foreach (var user in Input.SelectedUsers)
            {
                SelectedInTandemUsers.Add(_context.Users
                                          .FirstOrDefault(u => u.FullName == user));
            }


            RideEvent RideEventToUpdate = await _context.RideEvent
                                          .Include(r => r.RideLeaderAssignments)
                                          .ThenInclude(r => r.InTandemUser)
                                          .FirstOrDefaultAsync(m => m.ID == id);


            // takes list of names of ride leaders selected and adds them to a list of ride leader assignments
            var assignedLeaders = RideEventToUpdate.RideLeaderAssignments
                                  .Select(u => u.InTandemUser)
                                  .ToList();

            foreach (var leaderToBeAssigned in SelectedInTandemUsers)
            {
                // if the leader is not already assigned to the ride, assign them to the ride
                if (!assignedLeaders.Any(u => u.FullName.Equals(leaderToBeAssigned.FullName)))
                {
                    var NewLeaderAdded = new RideLeaderAssignment
                    {
                        InTandemUser   = leaderToBeAssigned,
                        InTandemUserID = leaderToBeAssigned.Id
                    };
                    RideEventToUpdate.RideLeaderAssignments.Add(NewLeaderAdded);
                }
            }
            // to indicate ride leader needs to be deleted, compare both lists
            // if the length of selected users is less than the number of assigned leaders
            // find which one is not in the selected users list and delete that assignment
            foreach (var assignedLeader in assignedLeaders)
            {
                // if a user is in the assignedUsers list but not in the selectedUsers list
                // remove the ride leader assignment containing that user
                if (!SelectedInTandemUsers.Any(u => u.FullName.Equals(assignedLeader.FullName)))
                {
                    RideLeaderAssignment RideLeaderAssignmentToBeRemoved = RideEventToUpdate.RideLeaderAssignments
                                                                           .SingleOrDefault(u => u.InTandemUser.FullName.Equals(assignedLeader.FullName));

                    RideEventToUpdate.RideLeaderAssignments.Remove(RideLeaderAssignmentToBeRemoved);
                }
            }


            RideEvent.Status        = EnumExtensionMethods.GetValueFromDescription <Status>(Input.SelectedStatus);
            RideEvent.EventType     = EnumExtensionMethods.GetValueFromDescription <EventType>(Input.SelectedEventType);
            RideEvent.MaxSignUpType = EnumExtensionMethods.GetValueFromDescription <MaxSignUpType>(Input.SelectedMaxSignUpType);
            // default entity tracking does not include navigation properties

            if (RideEventToUpdate == null)
            {
                return(NotFound());
            }
            // TruUpdateModelAsync is used to prevent overposting
            if (await TryUpdateModelAsync <RideEvent>(
                    RideEventToUpdate,
                    "RideEvent",
                    i => i.EventName, i => i.EventDate,
                    i => i.EventType, i => i.Description,
                    i => i.Location, i => i.Distance,
                    i => i.RideLeaderAssignments,
                    i => i.MaxSignup, i => i.Status
                    ))
            {
                _context.Update(RideEventToUpdate);
                await _context.SaveChangesAsync();

                return(RedirectToPage("./Index"));
            }

            return(RedirectToPage("./Index"));
        } // OnPostAsync