Skip to content

nss-sapphire-elephants/BangazonSite

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

56 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Welcome to Bangazon!

Overview

This version of Bangazon implements the Identity framework, and extends the base User object with the ApplicationUser model. It shows how to remove a model's property from the automatic model binding in a controller method by using ModelState.Remove(). Make sure you look in the DbInitializer class to see the product types that are seeded for you.

Setup

Pick on person from your team to follow these steps. No one else should touch anything at this point.

Git and SQL Server Configuration

  1. Clone this repository to your machine.
  2. Create a new repository on your team's Github organization named BangazonSite.
  3. Copy the connection string for your repo.
  4. From your project directory, execute the following commands
    git remote remove origin
    git remote add origin <paste your new Github URL here>
  5. Push up the master branch to your new remote origin
  6. Create a branch named initial-setup.
  7. Open Visual Studio and load the solution file

Seeding the Database

You will want to seed your database with some default values. Open the ApplicationDbContext.cs file and scroll all the way to the bottom. You will find the the following code.

modelBuilder.Entity<PaymentType> ().HasData (...)

The HasData() method lets you create one, or more, instances of a database model. Those instances will be turned into INSERT INTO SQL statements when you generate a migration either through the Package Manager Console with Add-Migration MigrationName or through the command line with dotnet ef migrations add MigrationName.

The boilerplate project has one user, two payment types, two product types, two products, one order, and two products on the order set up for you already.

Review that code with your team and if the team decides that they want more seeded data, add the new objects now.

Generating the Database

Once your appsettings are updated and you've entered in some seed data, you should generate your database.

  1. Go to the Package Manager Console in Visual Studio.
  2. Use the Add-Migration BangazonTables command.
  3. Once Visual Studio shows you the migration file, execute Update-Database to generate your tables.
  4. Use the SQL Server Object Explorer to verify that everything worked as expected.

Submit a PR

Push up your branch and submit a PR that your team lead will review and approve. No one else on the team can merge this PR until your team lead approves it.

Setup for Everyone

Once the initial setup is complete by the volunteer and the PR is approved by your team lead, the PR will get merged into master and now everyone else can pull the repository.

  1. Open Visual Studio and load the solution file
  2. Go to the Package Manager Console in Visual Studio.
  3. Execute Update-Database to generate your tables.
  4. Use the SQL Server Object Explorer to verify that everything worked as expected.

References for Tickets

Accessing the Authenticated User

In any of your controllers that need to access the currently authenticated user, for example the Order Summary screen or the New Product Form, you need to inject the UserManager into the controller. Here's the relevant code that you need.

Add a private field to your controller.

private readonly UserManager<ApplicationUser> _userManager;

In the constructor, inject the UserManager service.

public ProductsController(ApplicationDbContext ctx,
                          UserManager<ApplicationUser> userManager)
{
    _userManager = userManager;
    _context = ctx;
}

Then add the following private method to the controller.

private Task<ApplicationUser> GetCurrentUserAsync() => _userManager.GetUserAsync(HttpContext.User);

Once that is defined, any method that needs to see who the user is can invoke the method. Here's an example of when someone clicks the Purchase button for a product.

[Authorize]
public async Task<IActionResult> Purchase([FromRoute] int id)
{
    // Find the product requested
    Product productToAdd = await _context.Product.SingleOrDefaultAsync(p => p.ProductId == id);

    // Get the current user
    var user = await GetCurrentUserAsync();

    // See if the user has an open order
    var openOrder = await _context.Order.SingleOrDefaultAsync(o => o.User == user && o.PaymentTypeId == null);


    // If no order, create one, else add to existing order
}

Grouped Products by Product Type

One of the features you need to implement is a view that displays all of the product types as headers, with the first three products in that type listed beneath it. We are providing you a LINQ statement that will get you started.

Whomever tackles that ticket, this is the method that you will need to add to your ProductsController.cs.

public async Task<IActionResult> Types()
{
    var model = new ProductTypesViewModel();

    // Build list of Product instances for display in view
    // LINQ is awesome
    model.GroupedProducts = await (
        from t in _context.ProductType
        join p in _context.Product
        on t.ProductTypeId equals p.ProductTypeId
        group new { t, p } by new { t.ProductTypeId, t.Label } into grouped
        select new GroupedProducts
        {
            TypeId = grouped.Key.ProductTypeId,
            TypeName = grouped.Key.Label,
            ProductCount = grouped.Select(x => x.p.ProductId).Count(),
            Products = grouped.Select(x => x.p).Take(3)
        }).ToListAsync();

    return View(model);
}

In addition to that, add the following custom route to the bottom of your Startup.cs file.

routes.MapRoute ("types", "types",
    defaults : new { controller = "Products", action = "Types" });

Removing Items from Model Validation

One of the features you must implement is allowing customers to add products to sell. You'll need to remove the user from model validation to get it to work. Here's an example of something your team will need to do in Create() method in ProductsController.

// Remove the user from the model validation because it is
// not information posted in the form
ModelState.Remove("product.User");

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages