public bool LeaveComment(Comment comment) { MarketplaceContext context = new MarketplaceContext(); context.Comments.Add(comment); return(context.SaveChanges() > 0); }
public void Configuration(IAppBuilder app) { var context = new MarketplaceContext(); //context.Database.Initialize(true); ConfigureAuth(app); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder _app, IWebHostEnvironment _env, MarketplaceContext _context) { _context.Clear(); if (_env.IsDevelopment()) { _app.UseDeveloperExceptionPage(); } _app.UseRequestLogging(); _app.UseUnhandledExceptionHandling(); _app.UseSwagger(); _app.UseSwaggerUI(x => { x.SwaggerEndpoint("/swagger/v1/swagger.json", "Product API v1"); }); _app.UseRouting(); _app.UseAuthorization(); _app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); }
// //GET: User/Edit public ActionResult Edit(string id) { //Validate Id if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } using (var database = new MarketplaceContext()) { //Get user from database var user = database.Users.Where(u => u.Id == id).First(); //Check if user exists if (user == null) { return(HttpNotFound()); } //Create a view model var viewModel = new EditUserViewModel(); viewModel.User = user; viewModel.Roles = GetUserRoles(user, database); //Pass the model to the view return(View(viewModel)); } }
public ActionResult Edit(Category category, HttpPostedFileBase upload) { if (ModelState.IsValid) { using (var database = new MarketplaceContext()) { //if (upload != null && upload.ContentLength > 0) //{ // if (category.Files.Any(f => f.FileType == FileType.Avatar)) // { // database.Files.Remove(category.Files.First(f => f.FileType == FileType.Avatar)); // } // var avatar = new File // { // FileName = System.IO.Path.GetFileName(upload.FileName), // FileType = FileType.Avatar, // ContentType = upload.ContentType // }; // using (var reader = new System.IO.BinaryReader(upload.InputStream)) // { // avatar.Content = reader.ReadBytes(upload.ContentLength); // } // category.Files = new List<File> { avatar }; //} database.Entry(category).State = EntityState.Modified; database.SaveChanges(); return(RedirectToAction("Index")); } } return(View(category)); }
public AccountController(MarketplaceContext context, UserManager <User> userManager, SignInManager <User> signInManager, IWebHostEnvironment appEnvironment) { db = context; this.userManager = userManager; this.signInManager = signInManager; this.appEnvironment = appEnvironment; }
private List <Role> GetUserRoles(ApplicationUser user, MarketplaceContext db) { //Create user manager var userManager = Request.GetOwinContext().GetUserManager <ApplicationUserManager>(); //Get all application roles var roles = db.Roles.Select(r => r.Name).OrderBy(r => r).ToList(); //For each application role, chek if the user has it var userRoles = new List <Role>(); foreach (var roleName in roles) { var role = new Role { Name = roleName }; if (userManager.IsInRole(user.Id, roleName)) { role.IsSelected = true; } userRoles.Add(role); } //Return list with all roles return(userRoles); }
public ActionResult DeleteConfirmed(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } using (var database = new MarketplaceContext()) { // Get article from database var products = database.Products .Where(a => a.Id == id) .Include(c => c.Category) .Include(c => c.Files) .First(); // Check if article exists if (products == null) { return(HttpNotFound()); } // Delete article from database database.Files.Remove(products.Files.First(f => f.FileType == FileType.Avatar)); database.Products.Remove(products); database.SaveChanges(); // Redirect to index page return(RedirectToAction("Index")); } }
// // Get: Article/Delete public ActionResult Delete(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } using (var database = new MarketplaceContext()) { // Get article from database var product = database.Products .Where(a => a.Id == id) .Include(c => c.Category) .Include(c => c.Files) .First(); if (!IsUserAuthorizedToEdit(product)) { return(new HttpStatusCodeResult(HttpStatusCode.Forbidden)); } // Chek if article exists if (product == null) { return(HttpNotFound()); } // Pass article to view return(View(product)); } }
public TruckController( MarketplaceContext ctx, IMediator mediator) { _ctx = ctx; _mediator = mediator; }
public ActionResult Create(Category category, HttpPostedFileBase upload) { if (ModelState.IsValid) { using (var database = new MarketplaceContext()) { //if (upload == null) //{ // return RedirectToAction("Create"); //} //if (upload != null && upload.ContentLength > 0) //{ // var avatar = new File // { // FileName = System.IO.Path.GetFileName(upload.FileName), // FileType = FileType.Avatar, // ContentType = upload.ContentType // }; // using (var reader = new System.IO.BinaryReader(upload.InputStream)) // { // avatar.Content = reader.ReadBytes(upload.ContentLength); // } // category.Files = new List<File> { avatar }; //} database.Categories.Add(category); database.SaveChanges(); return(RedirectToAction("Index")); } } return(View(category)); }
public bool AddBid(Bid bid) { MarketplaceContext context = new MarketplaceContext(); context.Bids.Add(bid); return(context.SaveChanges() > 0); }
public void DeleteAuction(Auction auction) { MarketplaceContext context = new MarketplaceContext(); context.Entry(auction).State = System.Data.Entity.EntityState.Deleted; context.SaveChanges(); }
public void DeleteCategory(Category category) { MarketplaceContext context = new MarketplaceContext(); context.Entry(category).State = System.Data.Entity.EntityState.Deleted; context.SaveChanges(); }
public void SaveAuction(Auction auction) { MarketplaceContext context = new MarketplaceContext(); context.Auctions.Add(auction); context.SaveChanges(); }
public void SaveCategory(Category category) { MarketplaceContext context = new MarketplaceContext(); context.Categories.Add(category); context.SaveChanges(); }
public int SavePicture(Picture picture) { MarketplaceContext context = new MarketplaceContext(); context.Pictures.Add(picture); context.SaveChanges(); return(picture.ID); }
// // Get Category/List public ActionResult List() { using (var database = new MarketplaceContext()) { var categories = database.Categories.ToList(); return(View(categories)); } }
public CustomerServiceTests() { var options = new DbContextOptionsBuilder <MarketplaceContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options; _marketplaceContext = new MarketplaceContext(options); Seed(); _sut = new CustomerService(_marketplaceContext); }
public ActionResult Index() { MarketplaceContext db = new MarketplaceContext(); List <object> myModel = new List <object>(); myModel.Add(db.Categories.ToList()); myModel.Add(db.Products.ToList()); return(View(myModel)); }
public GalleyController( MarketplaceContext marketplaceContext, ImageService imageService, AuthorizationService authorizationService, QueryHelper queryHelper) { this.marketplaceContext = marketplaceContext; this.imageService = imageService; this.authorizationService = authorizationService; this.queryHelper = queryHelper; }
public ActionResult Edit(ProductViewModel model, HttpPostedFileBase upload) { // Check if model state is valid if (ModelState.IsValid) { using (var database = new MarketplaceContext()) { // Get article from database var product = database.Products .FirstOrDefault(a => a.Id == model.Id); // Set article properties model.Id = product.Id; model.Model = product.Model; model.Make = product.Make; model.Material = product.Material; model.Price = product.Price; model.Description = product.Description; model.Categories = database.Categories .OrderBy(c => c.Title) .ToList(); model.Files = product.Files; if (upload != null && upload.ContentLength > 0) { if (product.Files.Any(f => f.FileType == FileType.Avatar)) { database.Files.Remove(product.Files.First(f => f.FileType == FileType.Avatar)); } var avatar = new File { FileName = System.IO.Path.GetFileName(upload.FileName), FileType = FileType.Avatar, ContentType = upload.ContentType }; using (var reader = new System.IO.BinaryReader(upload.InputStream)) { avatar.Content = reader.ReadBytes(upload.ContentLength); } product.Files = new List <File> { avatar }; } // Save article state in database database.Entry(product).State = EntityState.Modified; database.SaveChanges(); } } // Redirect to the index page return(RedirectToAction("Index")); }
// // GET: Article/List public ActionResult List(int?id) { using (var database = new MarketplaceContext()) { //Get articles from database var products = database.Products .Include(a => a.Files) .ToList(); return(View(products)); } }
// // GET: User/List public ActionResult List() { using (var database = new MarketplaceContext()) { var users = database.Users.ToList(); var admins = GetAdminUserNames(users, database); ViewBag.Admins = admins; return(View(users)); } }
public ActionResult Create() { using (var database = new MarketplaceContext()) { var model = new ProductViewModel(); model.Categories = database.Categories .OrderBy(c => c.Title) .ToList(); return(View(model)); } }
public ActionResult ListCategories() { using (var database = new MarketplaceContext()) { var categories = database.Categories .Include(c => c.Products) .OrderBy(c => c.Title) .ToList(); return(View(categories)); } }
public void UpdateAuction(Auction auction) { MarketplaceContext context = new MarketplaceContext(); var existingAuction = context.Auctions.Find(auction.ID); context.AuctionPictures.RemoveRange(existingAuction.AuctionPictures); context.Entry(existingAuction).CurrentValues.SetValues(auction); context.AuctionPictures.AddRange(auction.AuctionPictures); context.SaveChanges(); }
private HashSet <string> GetAdminUserNames(List <ApplicationUser> users, MarketplaceContext context) { var userManager = new UserManager <ApplicationUser>( new UserStore <ApplicationUser>(context)); var admins = new HashSet <string>(); foreach (var user in users) { if (userManager.IsInRole(user.Id, "Admin")) { admins.Add(user.UserName); } } return(admins); }
public ActionResult ListProducts(int?categoryId) { if (categoryId == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } using (var database = new MarketplaceContext()) { var articles = database.Products .Where(a => a.CategoryId == categoryId) //.Include(a => a.Files) .ToList(); return(View(articles)); } }
private void SetUserRoles(EditUserViewModel viewModel, ApplicationUser user, MarketplaceContext context) { var userManager = HttpContext.GetOwinContext().GetUserManager <ApplicationUserManager>(); foreach (var role in viewModel.Roles) { if (role.IsSelected && !userManager.IsInRole(user.Id, role.Name)) { userManager.AddToRole(user.Id, role.Name); } else if (!role.IsSelected && userManager.IsInRole(user.Id, role.Name)) { userManager.RemoveFromRole(user.Id, role.Name); } } }