public User GetUserByUsername(string username) { using (var db = new ChushkaDbContext()) { return(db.Users.FirstOrDefault(u => u.Username == username)); } }
public bool IsUserExists(string username) { using (var db = new ChushkaDbContext()) { return(db.Users.Any(u => u.Username == username)); } }
public void Configure(IServerRoutingTable serverRoutingTable) { using (var context = new ChushkaDbContext()) { context.Database.EnsureCreated(); } }
public Startup() { using (var dbContext = new ChushkaDbContext()) { dbContext.Database.EnsureCreated(); } }
public User GetUserByUsernameAndPassword(string username, string password) { using (var db = new ChushkaDbContext()) { return(db.Users.FirstOrDefault(u => u.Username == username && u.Password == password)); } }
public UsersController(SignInManager <User> signInManager, UserManager <User> userManager, RoleManager <IdentityRole> roleManager, ChushkaDbContext dbContext) { this._signInManager = signInManager; this._userManager = userManager; this._roleManager = roleManager; this._dbContext = dbContext; }
public UsersService(UserManager <User> userManager, SignInManager <User> signInManager, RoleManager <IdentityRole> roleManager, ChushkaDbContext dbContext) { this.dbContext = dbContext; this.roleManager = roleManager; this.signInManager = signInManager; this.userManager = userManager; }
public UsersService(SignInManager <User> signInManager, UserManager <User> userManager, ChushkaDbContext context) { this.signInManager = signInManager; this.userManager = userManager; this.context = context; this.InitializeRoles(); }
public AccountService(ChushkaDbContext dbContext, UserManager <ChushkaUser> userManager, IEmailSender emailSender, ILogger <RegisterUserViewModel> _logger, SignInManager <ChushkaUser> signInManager) { this.dbContext = dbContext; this._userManager = userManager; this._emailSender = emailSender; this._signInManager = signInManager; this._logger = _logger; }
public RegisterModel( UserManager <ChushkaUser> userManager, SignInManager <ChushkaUser> signInManager, ChushkaDbContext chushkaDbContext) { _userManager = userManager; _signInManager = signInManager; this.chushkaDbContext = chushkaDbContext; }
public static void Main() { var db = new ChushkaDbContext(); using (db) { Console.WriteLine("Initialize/Update database..."); db.Database.Migrate(); if (!db.Roles.Any()) { Console.WriteLine("Seeding roles..."); db.Roles.Add(new Role() { Name = "Admin" }); db.Roles.Add(new Role() { Name = "User" }); db.SaveChanges(); } if (!db.ProductTypes.Any()) { Console.WriteLine("Seeding ProductTypes..."); db.ProductTypes.Add(new ProductType() { Name = "Food" }); db.ProductTypes.Add(new ProductType() { Name = "Domestic" }); db.ProductTypes.Add(new ProductType() { Name = "Health" }); db.ProductTypes.Add(new ProductType() { Name = "Cosmetic" }); db.ProductTypes.Add(new ProductType() { Name = "Other" }); db.SaveChanges(); } Console.WriteLine("Database ready!"); } var server = new WebServer(42420, new ControllerRouter(), new ResourceRouter()); MvcEngine.Run(server); }
public static void Main(string[] args) { WebServer server = new WebServer(8000, new ControllerRouter(), new ResourceRouter()); ChushkaDbContext context = new ChushkaDbContext(); ConfigureDataBase(context); MvcEngine.Run(server); }
public IActionResult Register(RegisterModel model) { if (this.User.IsAuthenticated) { return(this.RedirectToHomePage()); } if (!this.IsValidModel(model)) { this.ShowError(this.GetValidationSummary()); return(this.View()); } Role role; User user; using (var db = new ChushkaDbContext()) { role = db.Roles .Where(r => r.Name == DefaultRole) .FirstOrDefault(); if (role == null) { role = new Role() { Name = DefaultRole }; db.Roles.Add(role); } user = new User() { Username = model.Username, PasswordHash = PasswordUtilities.GetPasswordHash(model.Password), FullName = model.FullName, Email = model.Email, Role = role }; db.Users.Add(user); db.SaveChanges(); } var roles = new List <string>() { role.Name }; this.SignIn(user.Username, user.Id, roles); return(this.RedirectToHomePage()); }
public static void Main() { using (var db = new ChushkaDbContext()) { //db.Database.Migrate(); //db.Database.EnsureDeleted(); //db.Database.EnsureCreated(); } var webserver = new WebServer(8000, new ControllerRouter(), new ResourceRouter()); MvcEngine.Run(webserver); }
public static void Main(string[] args) { var server = new WebServer(1337, new ControllerRouter(), new ResourceRouter()); var dbContext = new ChushkaDbContext(); using (dbContext) { dbContext.Database.Migrate(); } MvcEngine.Run(server); }
public RegisterModel( UserManager <User> userManager, SignInManager <User> signInManager, ILogger <RegisterModel> logger, RoleManager <IdentityRole <int> > roleManager, ChushkaDbContext context) { _userManager = userManager; _signInManager = signInManager; _logger = logger; _roleManager = roleManager; this.context = context; }
public RegisterModel( UserManager <ChushkaUser> userManager, SignInManager <ChushkaUser> signInManager, ILogger <RegisterModel> logger, IEmailSender emailSender, ChushkaDbContext dbContext) { _userManager = userManager; _signInManager = signInManager; _logger = logger; _emailSender = emailSender; this.dbContext = dbContext; }
public IActionResult Index() { if (!this.User.IsAuthenticated) { this.ViewData["main"] = File.ReadAllText(TemplateDir + "/Home/GuestHomePage.html"); } else { IList <Product> products; using (var db = new ChushkaDbContext()) { products = db.Products .ToList(); } var productsResult = new StringBuilder(); productsResult.AppendLine(@"<div class=""row d-flex justify-content-around mt-4"">"); for (int i = 0; i < products.Count; i++) { var product = products[i]; productsResult.AppendLine( $@"<a href=""/products/details?id={product.Id}"" class=""col-md-2""> <div class=""product p-1 chushka-bg-color rounded-top rounded-bottom""> <h5 class=""text-center mt-3"">{product.Name}</h5> <hr class=""hr -1 bg-white"" /> <p class=""text -white text-center"" > {GetProductDescription(product.Description)} </p> <hr class=""hr -1 bg-white""/> <h6 class=""text-center text-white mb-3"">${product.Price.ToString("0.##")}</h6> </div> </a>"); if ((i + 1) % 5 == 0) { productsResult.AppendLine(@"</div>"); productsResult.AppendLine(@"<div class=""row d-flex justify-content-around mt-4"">"); } } productsResult.AppendLine(@"</div>"); this.ViewData["main"] = File.ReadAllText(TemplateDir + "/Home/UserHomePage.html"); this.ViewData["username"] = this.User.Name; this.ViewData["products"] = productsResult.ToString(); } return(this.View()); }
public static void Main() { using (var db = new ChushkaDbContext()) { db.Database.Migrate(); // Uncommet the next line to seed the database! // SeedDatabase(db); } var server = new SoftUni.WebServer.Server.WebServer(1337, new ControllerRouter(), new ResourceRouter()); MvcEngine.Run(server); }
static void Main(string[] args) { int port = 8000; var context = new ChushkaDbContext(); context.Database.Migrate(); Console.WriteLine("Database Ready!"); var server = new WebServer(port, new ControllerRouter(), new ResourceRouter()); MvcEngine.Run(server, context); }
public IActionResult Create(CreateProductModel model) { if (!this.IsAdmin) { return(this.RedirectToHomePage()); } if (!this.IsValidModel(model)) { this.ShowError(this.GetValidationSummary()); return(this.View()); } ProductType productType; Product product; using (var db = new ChushkaDbContext()) { productType = db .ProductTypes .FirstOrDefault(pt => pt.Name == model.ProductType); if (productType == null) { productType = new ProductType() { Name = model.ProductType }; db.ProductTypes.Add(productType); } product = new Product() { Name = model.Name, Price = model.Price, Description = model.Description, Type = productType }; db.Products.Add(product); db.SaveChanges(); } return(this.RedirectToHomePage()); }
private static void SeedDatabase(ChushkaDbContext db) { var roles = new List <Role>() { new Role() { Name = "User" }, new Role() { Name = "Admin" } }; var productTypes = new List <ProductType>() { new ProductType() { Name = "Food" }, new ProductType() { Name = "Domestic" }, new ProductType() { Name = "Health" }, new ProductType() { Name = "Cosmetic" }, new ProductType() { Name = "Other" }, }; db.Roles.AddRange(roles); db.ProductTypes.AddRange(productTypes); db.SaveChanges(); Console.WriteLine("Database Seeded!"); }
public IActionResult Edit(int id) { if (!IsAdmin) { return(this.RedirectToHomePage()); } Product product; ProductType[] productTypes; using (var db = new ChushkaDbContext()) { product = db.Products .Include(p => p.Type) .FirstOrDefault(p => p.Id == id); if (product == null) { return(this.RedirectToHomePage()); } productTypes = db.ProductTypes.ToArray(); } this.ViewData["productId"] = product.Id.ToString(); this.ViewData["name"] = product.Name; this.ViewData["price"] = product.Price.ToString("0.##"); this.ViewData["description"] = product.Description; for (int i = 0; i < productTypes.Length; i++) { if (product.Type.Name == productTypes[i].Name) { this.ViewData[$"isChecked{i + 1}"] = "checked"; } else { this.ViewData[$"isChecked{i + 1}"] = string.Empty; } } return(this.View()); }
public IActionResult Edit(EditProductModel model) { if (!IsAdmin) { return(this.RedirectToHomePage()); } if (!this.IsValidModel(model)) { this.ShowError(this.GetValidationSummary()); this.ViewData["productId"] = model.Id.ToString(); this.ViewData["name"] = model.Name; this.ViewData["price"] = model.Price.ToString("0.##"); this.ViewData["description"] = model.Description; return(this.View()); } using (var db = new ChushkaDbContext()) { var productType = db .ProductTypes .FirstOrDefault(pt => pt.Name == model.ProductType); if (productType == null) { productType = new ProductType() { Name = model.ProductType }; db.ProductTypes.Add(productType); } var product = db.Products.FirstOrDefault(p => p.Id == model.Id); product.Name = model.Name; product.Price = model.Price; product.Description = model.Description; product.Type = productType; db.SaveChanges(); } return(this.RedirectToHomePage()); }
public IActionResult Login(LoginModel model) { if (this.User.IsAuthenticated) { return(this.RedirectToHomePage()); } if (!this.IsValidModel(model)) { this.ShowError(this.GetValidationSummary()); return(this.View()); } UserLoginViewModel user; using (var db = new ChushkaDbContext()) { user = db.Users .Select(u => new UserLoginViewModel() { Id = u.Id, Username = u.Username, PasswordHash = u.PasswordHash, RoleName = u.Role.Name }) .FirstOrDefault(u => u.Username == model.Username); } if (user == null || user.PasswordHash != PasswordUtilities.GetPasswordHash(model.Password)) { this.ShowError("Your login details are incorrect!"); return(this.View()); } var roles = new List <string>() { user.RoleName }; this.SignIn(user.Username, user.Id, roles); return(this.RedirectToHomePage()); }
public IActionResult Order(int id) { if (!this.User.IsAuthenticated) { return(this.RedirectToHomePage()); } using (var db = new ChushkaDbContext()) { db.Orders.Add(new Order() { ClientId = this.User.Id, ProductId = id, OrderedOn = DateTime.Now }); db.SaveChanges(); } return(this.IsAdmin ? this.RedirectToAction("/orders/all") : this.RedirectToHomePage()); }
public IActionResult All() { if (!this.IsAdmin) { return(this.RedirectToHomePage()); } IList <AllOrdersViewModel> orders; using (var db = new ChushkaDbContext()) { orders = db.Orders .Select(o => new AllOrdersViewModel() { OrderId = o.Id.ToString(), Customer = o.Client.FullName, Product = o.Product.Name, OrderedOn = o.OrderedOn.ToString("HH:mm dd/MM/yyyy") }) .ToList(); } var ordersResult = new StringBuilder(); for (int i = 0; i < orders.Count; i++) { var order = orders[i]; ordersResult.AppendLine($@"<tr> <th scope=""row"">{i + 1}</th> <td>{order.OrderId}</td> <td>{order.Customer}</td> <td>{order.Product}</td> <td>{order.OrderedOn}</td> </tr>"); } this.ViewData["orders"] = ordersResult.ToString(); return(this.View()); }
public void AddUser(string username, string password, string fullName, string email) { using (var db = new ChushkaDbContext()) { var user = new User { Username = username, Password = password, FullName = fullName, Email = email, Role = Role.User }; if (!db.Users.Any()) { user.Role = Role.Admin; } db.Users.Add(user); db.SaveChanges(); } }
public IActionResult Details(int id) { if (!this.User.IsAuthenticated) { return(this.RedirectToHomePage()); } ProductDetailsViewModel productDetails; using (var db = new ChushkaDbContext()) { productDetails = db.Products .Where(p => p.Id == id) .Select(p => new ProductDetailsViewModel() { Name = p.Name, Type = p.Type.Name, Price = p.Price, Description = p.Description }) .FirstOrDefault(); if (productDetails == null) { return(this.RedirectToHomePage()); } } this.ViewData["name"] = productDetails.Name; this.ViewData["type"] = productDetails.Type; this.ViewData["price"] = productDetails.Price.ToString("0.##"); this.ViewData["description"] = productDetails.Description; this.ViewData["actions"] = this.IsAdmin ? File.ReadAllText(TemplateDir + AdminDetailsActionsTemplate) : File.ReadAllText(TemplateDir + UserDetailsActionsTemplate); this.ViewData["productId"] = id.ToString(); return(this.View()); }
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ChushkaDbContext dbContext, RoleManager <ApplicationUserRole> roleManager) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseDatabaseErrorPage(); } else { app.UseExceptionHandler("/Home/Error"); app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseCookiePolicy(); app.UseAuthentication(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); if (env.IsDevelopment()) { dbContext.Database.Migrate(); } DatabaseSeeder.Seed(dbContext, roleManager); }