# codewithiulian/coffeeshopper-is4 architecture

> Architecture diagram of the GitHub repository codewithiulian/coffeeshopper-is4, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-25). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/codewithiulian/coffeeshopper-is4
- Repository: https://github.com/codewithiulian/coffeeshopper-is4

## Overview

##### Project type and purpose
- This is a full-stack ASP.NET Core application demonstrating integration of IdentityServer 4 with ASP.NET Identity.
- It consists of four main deliverables:
- A Blazor front-end (“Client”)
- A REST API (“API”) serving coffee-shop data
- A DataAccess layer (EF Core contexts and migrations)
- An IdentityServer host (“Server”) for authentication/authorization

##### File structure
- Top-level projects:
- Client/ – Blazor UI (razor pages, token service)
- API/ – ASP.NET Core Web API (Controllers, Models, Services)
- DataAccess/ – EF Core DbContext, Entities, Migrations
- Server/ – IdentityServer4 host with ASP.NET Identity integration, Quickstart UI, multiple EF contexts (Identity, Configuration, Persisted Grants)

##### README
- States the purpose: integrate ASP.NET Identity with IdentityServer 4
- Implies OAuth2/OIDC flows will secure the API and UI

##### Main components
a. Frontend (Client)
- Blazor WebAssembly or Server-side app
- TokenService obtains and stores access tokens via IdentityServer OIDC endpoints
b. IdentityServer (Server)
- Hosts /connect/authorize, /connect/token, /connect/userinfo, etc.
- Underlying ASP.NET Identity user store (AspNetIdentityDbContext)
- ConfigurationDbContext (client, resources)
- PersistedGrantDbContext (tokens, consents)
c. CoffeeShop API (API)
- Exposes /api/coffeeshops endpoints
- Validates bearer tokens against IdentityServer
- Delegates business logic to CoffeeShopService and persistence to DataAccess
d. DataAccess (DataAccess)
- ApplicationDbContext with CoffeeShop entity
- EF Core migrations
- Connects to a SQL database
e. Database(s)
- One DB for application data
- One or more DBs (or schemas) for IdentityServer’s configuration, operational, and identity tables

##### Relationships and interactions
- User → Client UI: browser loads Blazor app
- Client → IdentityServer: OIDC authorize code flow to sign in, receive id_token and access_token
- Client → API: each API request includes the Bearer access_token
- API → DataAccess/ApplicationDbContext → Application DB: CRUD on CoffeeShop
- IdentityServer → AspNetIdentityDbContext → Identity DB: user lookup, password checks
- IdentityServer → ConfigurationDbContext → Config DB: client/resource settings
- IdentityServer → PersistedGrantDbContext → Operational DB: store tokens, consents

##### Architectural patterns and design principles
- Layered architecture: UI / API / Business services / Data access
- Separation of concerns: authentication & token service separated from data-oriented API
- Dependency injection of services (ICoffeeShopService, ITokenService)
- Use of EF Core Code-First migrations

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Actor
    User["User/Browser"]:::external

    %% Presentation Layer
    subgraph "Client (Blazor UI)"
        direction TB
        ClientApp["Blazor Client"]:::frontend
        TokenSvc["TokenService"]:::service
        ITokenSvc["ITokenService"]:::service
        IdentitySettings["IdentityServerSettings"]:::service
        ClientApp -->|"uses"| TokenSvc
        TokenSvc -->|"implements"| ITokenSvc
        TokenSvc -->|"configures"| IdentitySettings
    end

    %% Authentication Layer
    subgraph "IdentityServer4 Host (Server)"
        direction TB
        IdSrv["IdentityServer4 Host"]:::auth
        subgraph "DbContexts"
            direction TB
            AspIdDb["AspNetIdentityDbContext"]:::db
            ConfigDb["ConfigurationDbContext"]:::db
            PersistDb["PersistedGrantDbContext"]:::db
        end
        IdSrv -->|"EF Core Reads/Writes"| AspIdDb
        IdSrv -->|"EF Core Reads/Writes"| ConfigDb
        IdSrv -->|"EF Core Reads/Writes"| PersistDb
    end

    %% API Layer
    subgraph "CoffeeShop API (API)"
        direction TB
        ApiApp["CoffeeShop API"]:::api
        CSController["CoffeeShopController"]:::service
        CSService["CoffeeShopService"]:::service
        ICSService["ICoffeeShopService"]:::service
        ApiApp -->|"routes to"| CSController
        CSController -->|"calls"| CSService
        CSService -->|"implements"| ICSService
    end

    %% Data Access Layer
    subgraph "DataAccess Layer"
        direction TB
        DataAccess["DataAccess"]:::data
        AppDbCtx["ApplicationDbContext"]:::db
    end

    %% Databases
    subgraph "Databases"
        direction TB
        AppDB["Application DB"]:::db
        IdDB["Identity DB"]:::db
        ConfDB["Config DB"]:::db
        PGrantsDB["Persisted Grants DB"]:::db
    end

    %% Relationships
    User -->|"Loads Blazor App (HTTPS)"| ClientApp
    ClientApp -->|"OIDC Auth Code Flow\n(/connect/authorize, /connect/token)"| IdSrv
    ClientApp -->|"Bearer token\n(Authorization: Bearer)"| ApiApp
    ApiApp -->|"Delegates to"| DataAccess
    DataAccess -->|"CRUD CoffeeShop\n(Entity Framework)"| AppDB
    AspIdDb -->|"SQL"| IdDB
    ConfigDb -->|"SQL"| ConfDB
    PersistDb -->|"SQL"| PGrantsDB

    %% Click Events
    click ClientApp "https://github.com/codewithiulian/coffeeshopper-is4/tree/main/Client/"
    click TokenSvc "https://github.com/codewithiulian/coffeeshopper-is4/blob/main/Client/Services/TokenService.cs"
    click ITokenSvc "https://github.com/codewithiulian/coffeeshopper-is4/blob/main/Client/Services/ITokenService.cs"
    click IdentitySettings "https://github.com/codewithiulian/coffeeshopper-is4/blob/main/Client/Services/IdentityServerSettings.cs"
    click CSController "https://github.com/codewithiulian/coffeeshopper-is4/blob/main/API/Controllers/CoffeeShopController.cs"
    click CSService "https://github.com/codewithiulian/coffeeshopper-is4/blob/main/API/Services/CoffeeShopService.cs"
    click ICSService "https://github.com/codewithiulian/coffeeshopper-is4/blob/main/API/Services/ICoffeeShopService.cs"
    click ApiApp "https://github.com/codewithiulian/coffeeshopper-is4/tree/main/API/"
    click AppDbCtx "https://github.com/codewithiulian/coffeeshopper-is4/blob/main/DataAccess/Data/ApplicationDbContext.cs"
    click DataAccess "https://github.com/codewithiulian/coffeeshopper-is4/tree/main/DataAccess/"
    click AspIdDb "https://github.com/codewithiulian/coffeeshopper-is4/blob/main/Server/Data/AspNetIdentityDbContext.cs"
    click IdSrv "https://github.com/codewithiulian/coffeeshopper-is4/tree/main/Server/"
    click ConfigDb "https://github.com/codewithiulian/coffeeshopper-is4/tree/main/Server/Migrations/ConfigurationDb/"
    click PersistDb "https://github.com/codewithiulian/coffeeshopper-is4/blob/main/Server/Migrations/PersistedGrantDbContextModelSnapshot.cs"

    %% Styles
    classDef external fill:#ffffff,stroke:#000000,stroke-width:1px;
    classDef frontend fill:#add8e6,stroke:#000000;
    classDef auth fill:#90ee90,stroke:#000000;
    classDef api fill:#ffa500,stroke:#000000;
    classDef data fill:#dda0dd,stroke:#000000;
    classDef db fill:#d3d3d3,stroke:#000000;
    classDef service fill:#f5f5f5,stroke:#000000;
```
