Example #1
0
        public FarmerBlog GetById(int id)
        {
            using (var context = new FarmerContext())
            {
                var response = (from i in context.Blogs
                                where i.Id == id
                                select new FarmerBlog
                {
                    Id = i.Id,
                    CreatedDate = i.CreatedDate,
                    Description = i.Description,
                    ImagePath = i.ImagePath,
                    QueueNumber = i.QueueNumber,
                    Title = i.Title,
                    UserId = i.UserId,
                    Comments = (from c in i.Comments
                                where i.Id == c.BlogId
                                select new FarmerComment
                    {
                        BlogId = c.BlogId,
                        Email = c.Email,
                        CommentDate = c.CommentDate,
                        Id = c.Id,
                        Comment = c.Comment,
                        Name = c.Name,
                        Surname = c.Surname,
                        UserId = c.UserId,
                        WebSite = c.WebSite
                    }).ToList()
                }).FirstOrDefault();

                return(response);
            }
        }
Example #2
0
        public static void ConfigureServices(IServiceCollection services, IConfiguration configuration)
        {
            string connectionString = configuration.GetConnectionString("HappyFarmerContext");

            FarmerContext.SetConnectionString(connectionString);
            services.AddDbContext <FarmerContext>(opts => opts.UseMySQL(connectionString, i => i.MigrationsAssembly("HappyFarmer.DataAccess")));

            services.AddDistributedMemoryCache();

            services.AddSession(options =>
            {
                options.Cookie.Name     = ".HappyFarmer.Session";
                options.IdleTimeout     = TimeSpan.FromMinutes(2);
                options.Cookie.HttpOnly = true; options.Cookie.IsEssential = true;
            });
        }
Example #3
0
        public static void Seed()
        {
            var context = new FarmerContext();

            if (context.Database.GetPendingMigrations().Count() > 0)
            {
                context.Database.Migrate();
            }

            if (context.Database.GetPendingMigrations().Count() == 0)
            {
                if (context.FarmerCategory.Count() == 0)
                {
                    context.FarmerCategory.AddRange(Categories);
                }

                if (context.FarmerUser.Count() == 0)
                {
                    context.FarmerUser.AddRange(farmerUsers);
                }

                if (context.Products.Count() == 0)
                {
                    context.Products.AddRange(Products);
                    context.AddRange(productCategory);
                }
                if (context.Blogs.Count() == 0)
                {
                    context.Blogs.AddRange(farmerBlogs);
                }

                if (context.AboutUs.Count() == 0)
                {
                    context.AboutUs.AddRange(farmerAboutUs);
                }
                if (context.Orders.Count() == 0)
                {
                    context.Orders.AddRange(farmerOrders);
                }

                if (context.OrderItems.Count() == 0)
                {
                    context.OrderItems.AddRange(farmerOrderItems);
                }
                context.SaveChanges();
            }
        }
Example #4
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 (UsersContext db = new UsersContext())
                using (FarmerContext db = new FarmerContext())
                {
                    UserProfile 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));
        }
Example #5
0
        public List <FarmerProduct> GetAll(int?page = 0, int?pageSize = 0)
        {
            using (var context = new FarmerContext())
            {
                if (page == 0 && pageSize == 0)
                {
                    return(context.Products
                           .Where(i => i.PermissionToSell == true).Include(i => i.ProductCategories).Include(i => i.ProductComments).Include(i => i.MultipleProductImages)
                           .ToList());
                }

                else
                {
                    var products = context.Products.AsQueryable();

                    products = products
                               .Include(i => i.ProductCategories)
                               .Where(i => i.PermissionToSell == true);

                    return(products.Skip((int)((page - 1) * pageSize)).Take((int)pageSize).ToList());
                }
            }
        }
Example #6
0
        public List <FarmerBlog> GetAll(int?page = 0, int?pageSize = 0)
        {
            using (var context = new FarmerContext())
            {
                if (page == 0 && pageSize == 0)
                {
                    var response = context.Blogs
                                   .OrderBy(i => i.QueueNumber).AsNoTracking().ToList();

                    return(response);
                }

                else
                {
                    var blogs = context.Blogs.AsQueryable();

                    blogs = blogs
                            .OrderByDescending(i => i.CreatedDate)
                            .AsNoTracking();

                    return(blogs.Skip((int)((page - 1) * pageSize)).Take((int)pageSize).ToList());
                }
            }
        }
            public SimpleMembershipInitializer()
            {
                //Database.SetInitializer<UsersContext>(null);
                Database.SetInitializer <FarmerContext>(null);

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

                    WebSecurity.InitializeDatabaseConnection("FarmerContext", "UserProfile", "UserId", "UserName", autoCreateTables: true);
                }
                catch (Exception 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);
                }
            }
 public BarnController()
 {
     _ctx = new FarmerContext();
 }
 public PigController()
 {
     _ctx = new FarmerContext();
 }
 public TemperatureSensorController()
 {
     _ctx = new FarmerContext();
 }