Esempio n. 1
0
        // POST api/<controller>
        public HttpResponseMessage Post([FromBody]NewOrderModel newOrder)
        {
            var emailAddress = User.Identity.Name;
            var dealOptionGuid = default(Guid);

            Guid.TryParse(newOrder.DealOptionId, out dealOptionGuid);

            using (var context = new DealsContext())
            {
                var user = context.Users.First(u => u.EmailAddress == emailAddress);
                var dealOption = context.DealOptions.First(d => d.Id == dealOptionGuid);
                var attributes = newOrder.SelectedAttributes
                    .Select(attr=> { return Guid.Parse(attr.Value); })
                    .Select(attr =>
                    {
                        return context.DealAttributes.First(a => a.Id.Equals(attr));
                    });

                var cart = new Cart
                {
                    Id = Guid.NewGuid(),
                    DealOption = dealOption,
                    DealAttributes = attributes.ToList(),
                    Quantity = 1,
                    User = user
                };

                context.Carts.Add(cart);
                context.SaveChanges();
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }
Esempio n. 2
0
        public static void Seed(DealsContext context)
        {
            var categories = CreateDealCategories(context);
            var merchants = CreateMerchants(context);
            var deals = CreateDeals(context, categories, merchants);
            var users = CreateUsers(context);
            CreateDealOptions(context, deals);
            CreateDealComments(context, deals, users);
            CreateDealImages(context, deals);

            context.SaveChanges();
        }
Esempio n. 3
0
        public ActionResult Index()
        {
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

            using (var context = new DealsContext())
            {
                var model = new IndexViewModel();
                //model.FeaturedDeals = context.Deals.Include("Merchant").Include("Pictures").Where(d => d.IsFeatured).Take(3).ToList();
                //model.NewDeals = context.Deals.Include("Merchant").Include("Pictures").OrderByDescending(d => d.DateAdded).Take(16).ToList();
                //model.PopularDeals = new PopularDealsViewModel(context.Deals.Include("Pictures").OrderByDescending(d => d.DateAdded).Take(16).ToList());
                //model.Categories = context.DealCategories.ToList();

                return View(model);
            }
        }
Esempio n. 4
0
        private static IList<DealCategory> CreateDealCategories(DealsContext context)
        {
            var categories = new string[] { "Food & Drink", "Beauty & Wellness", "Travel", "Goods", "Shopping" };
            var dealCategories = new List<DealCategory>();

            foreach (var category in categories)
            {
                var dealCategory = new DealCategory
                {
                    Id = Guid.NewGuid(),
                    Name = category
                };

                dealCategories.Add(dealCategory);
                context.DealCategories.Add(dealCategory);
            }

            return dealCategories;
        }
Esempio n. 5
0
        private static void CreateDealImages(DealsContext context, IList<Deal> deals)
        {
            var random = new Random();

            foreach (var deal in deals)
            {
                for (int i = 0; i < 3; i++)
                {
                    var randNumber = random.Next(0, 18);
                    var image = new DealImages
                    {
                        Id = Guid.NewGuid(),
                        Order = i,
                        RelativeUrl = string.Format("{0}.jpg", randNumber),
                        Deal = deal
                    };

                    context.DealImages.Add(image);
                }
            }
        }
Esempio n. 6
0
        private static void CreateDealComments(DealsContext context, IList<Deal> deals, IList<User> users)
        {
            var random = new Random();

            foreach (var deal in deals)
            {
                for (int i = 0; i < 3; i++)
                {
                    var randomUser = users[random.Next(0, users.Count)];

                    var comment = new DealComment
                    {
                        Id = Guid.NewGuid(),
                        DatePosted = DateTime.UtcNow,
                        Message = string.Format("Message {0}", i),
                        Poster = randomUser,
                        Deal = deal
                    };

                    context.DealComments.Add(comment);
                }
            }
        }
            public SimpleMembershipInitializer()
            {
                Database.SetInitializer<DealsContext>(null);

                try
                {
                    using (var context = new DealsContext())
                    {
                        if (!context.Database.Exists())
                        {
                            // Create the SimpleMembership database without Entity Framework migration schema
                            ((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
                            SampleData.Seed(context);
                        }
                    }

                    WebSecurity.InitializeDatabaseConnection("DefaultConnection", "User", "UserId", "Username", autoCreateTables: true);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex);
                }
            }
Esempio n. 8
0
        private static void CreateDealOptions(DealsContext context, List<Deal> deals)
        {
            var random = new Random();
            var randomNumber = random.Next(0, 5);

            foreach (var deal in deals)
            {
                deal.DealOptions = new List<DealOption>();
                for (int i = 0; i < randomNumber; i++)
                {
                    var dealOption = new DealOption
                    {
                        Id = Guid.NewGuid(),
                        RegularPrice = 10,
                        SpecialPrice = 5,
                        ShortTitle = string.Format("Option {0}", i)
                    };

                    dealOption.Attributes = new List<DealAttribute>();
                    dealOption.Attributes.Add(new DealAttribute
                    {
                        Id = Guid.NewGuid(),
                        Name = "Color",
                        Value = "Black"
                    });
                    dealOption.Attributes.Add(new DealAttribute
                    {
                        Id = Guid.NewGuid(),
                        Name = "Color",
                        Value = "Blue"
                    });
                    dealOption.Attributes.Add(new DealAttribute
                    {
                        Id = Guid.NewGuid(),
                        Name = "Color",
                        Value = "Red"
                    });
                    dealOption.Attributes.Add(new DealAttribute
                    {
                        Id = Guid.NewGuid(),
                        Name = "Size",
                        Value = "S"
                    });
                    dealOption.Attributes.Add(new DealAttribute
                    {
                        Id = Guid.NewGuid(),
                        Name = "Size",
                        Value = "M"
                    });
                    dealOption.Attributes.Add(new DealAttribute
                    {
                        Id = Guid.NewGuid(),
                        Name = "Size",
                        Value = "L"
                    });

                    deal.DealOptions.Add(dealOption);
                }
            }
        }
Esempio n. 9
0
        private static IList<User> CreateUsers(DealsContext context)
        {
            var users = new List<User>();

            for (int i = 0; i < 10; i++)
            {
                var user = new User
                {
                    UserId = i,
                    Username = string.Format("User{0}", i),
                    EmailAddress = string.Format("user{0}@email.com", i),
                    AddressLine1 = "addressline1",
                    State = "state",
                    City = "city",
                    Street = "street",
                    FirstName = "firstname",
                    LastName = "lastname",
                    ZipCode = "zip"
                };

                users.Add(user);
                context.Users.Add(user);
            }

            return users;
        }
Esempio n. 10
0
        private static IList<Merchant> CreateMerchants(DealsContext context)
        {
            var merchants = new List<Merchant>();

            for (int i = 0; i < 10; i++)
            {
                var merchant = new Merchant
                {
                    BusinessRegNumber = "RegNumber",
                    EmailAddress = "*****@*****.**",
                    Id = Guid.NewGuid(),
                    Website = "http://www.website.com",
                    PhoneNumber = "010-20318122",
                    Name = string.Format("Merchant {0}", i),
                    About = "About merchant",
                    Address = "address"
                };

                merchants.Add(merchant);
                context.Merchants.Add(merchant);
            }

            return merchants;
        }
Esempio n. 11
0
        private static List<Deal> CreateDeals(DealsContext context, IList<DealCategory> categories, IList<Merchant> merchants)
        {
            var description = GetDescription();
            var fineprint = GetFinePrint();
            var highlight = GetHighlight();

            var random = new Random();

            var deals = new List<Deal>();

            for (int i = 0; i < 200; i++)
            {
                var isFeatured = false;

                if (i < 3)
                {
                    isFeatured = true;
                }

                var randomCategory = categories[random.Next(0, categories.Count)];
                var randomMerchant = merchants[random.Next(0, merchants.Count)];

                var deal = new Deal
                {
                    Id = Guid.NewGuid(),
                    SKU = string.Format("SKU{0}{1}", randomCategory.Name, i.ToString()),
                    Merchant = randomMerchant,
                    RegularPrice = 10,
                    SpecialPrice = 5.50,
                    ShortTitle = string.Format("Sukiyaki Buffet Lunch at IOI Mall Puchong {0}{1}", randomCategory.Name, i.ToString()),
                    FinePrint = fineprint,
                    Highlight = highlight,
                    StartTime = DateTime.UtcNow,
                    EndTime = DateTime.UtcNow.AddDays(30),
                    IsFeatured = isFeatured,
                    DateAdded = DateTime.UtcNow,
                    LongTitle = "[41% Off] Sukishi: Sukiyaki Buffet Lunch at IOI Mall Puchong for RM24.90",
                    LongDescription = description,
                    ShortDescription = "Authentic Japanese sukiyaki in a casual dining atmosphere.",
                    CanonicalUrl = string.Format("url-for-product-{0}", i),
                    Category = randomCategory,
                    Status = DealStatus.Published
                };

                deals.Add(deal);
                context.Deals.Add(deal);
            }

            return deals;
        }
Esempio n. 12
0
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    WebSecurity.CreateUserAndAccount(model.UserName, model.Password);

                    using (var context = new DealsContext())
                    {
                        var user = new User
                        {
                            Username = model.UserName,
                            EmailAddress = model.UserName
                        };

                        context.Users.Add(user);
                        context.SaveChanges();
                    }

                    WebSecurity.Login(model.UserName, model.Password);

                    return RedirectToAction("Index", "Home");
                }
                catch (MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
Esempio n. 13
0
        public ActionResult ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl)
        {
            string provider = null;
            string providerUserId = null;

            if (User.Identity.IsAuthenticated || !OAuthWebSecurity.TryDeserializeProviderUserId(model.ExternalLoginData, out provider, out providerUserId))
            {
                return RedirectToAction("Manage");
            }

            if (ModelState.IsValid)
            {
                // Insert a new user into the database
                using (DealsContext db = new DealsContext())
                {
                    var user = db.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower());
                    // Check if user already exists
                    if (user == null)
                    {
                        // Insert name into the profile table
                        db.UserProfiles.Add(new UserProfile { UserName = model.UserName });
                        db.SaveChanges();

                        OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName);
                        OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false);

                        return RedirectToLocal(returnUrl);
                    }
                    else
                    {
                        ModelState.AddModelError("UserName", "User name already exists. Please enter a different user name.");
                    }
                }
            }

            ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName;
            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }
Esempio n. 14
0
 public static IEnumerable<Deal> SuggestPopularDeals(HttpContextBase httpContext, DealsContext dealsContext, int max = 10)
 {
     return dealsContext.Deals.Include("Pictures").Take(max);
 }