# kirillius7/urlservice architecture

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

- Interactive diagram: https://gitdiagram.com/kirillius7/urlservice
- Repository: https://github.com/kirillius7/urlservice

## Overview

##### Project Type and Purpose
- This is a full-stack URL Shortener application.
- Backend: ASP.NET MVC (C#) with Entity Framework (Code-First) and Identity for auth.
- Frontend: React SPA built with Vite, using hooks and Fetch API to consume the backend.
- Purpose: allow users to register/login, shorten long URLs, view/manage their own (or all, for admins) short links, and redirect via the shortened paths.

##### Main Components
a. Frontend (React/Vite)
- Entry: src/main.jsx → App.jsx
- Components:
- Login.jsx / Registration.jsx (auth forms)
- UrlsTable.jsx (list of URLs)
- UrlInfo.jsx (details view)
- Create URL form (noted in service/api.js)
- Service Layer: src/service/api.js (all HTTP calls to the backend)
- Assets & CSS: under src/assets and component-level CSS.
b. Backend (ASP.NET MVC)
- Program.cs & launchSettings.json (app startup, Kestrel/IIS settings)
- Controllers:
- AccountController.cs (login, registration, token or cookie flows)
- UrlsController.cs (API endpoints: Create, Read, Delete URLs, Redirect short URL)
- HomeController.cs (MVC views for non-SPA parts if any)
- Models & Data:
- AppUser.cs (Identity user model)
- ShortUrl.cs (URL entity)
- CreateUrlDto.cs (DTO for creating URLs)
- AppDbContext.cs (EF Core DbContext)
- Migrations folder (code-first snapshots & migration classes)
- Service Layer: IUrlService.cs + UrlServiceClass.cs (business logic: check duplicates, create short codes, fetch details)
- Views: MVC Razor views under Views/Home and Views/Shared (fallback or pre-React pages)
- Static Assets: wwwroot (Bootstrap, jQuery validation, site.js/css)
c. Database
- SQL Server (or configured provider) storing AspNetIdentity tables and ShortUrls table.
d. Tests
- UrlService.Tests project with unit tests for service logic or controllers.

##### Component Interactions & Data Flow
- User interacts with React UI in browser → actions call service/api.js → HTTP requests to the ASP.NET MVC API endpoints (UrlsController, AccountController).
- Controllers delegate to UrlServiceClass (IUrlService) → business rules, mapping between DTOs and EF entities.
- UrlServiceClass uses AppDbContext to read/write to the database.
- EF Core Migrations ensure schema (InitialCreate + IdentityTables).
- For redirect: incoming GET /u/{shortCode} hits UrlsController.Redirect → service finds original URL → returns HTTP 302.
- Auth flows: Login/Register endpoints set cookies or tokens; API endpoints validate roles (Admin vs. User) before allowing Create/Delete.

##### Architectural Patterns & Principles
- MVC (Controllers / Views / Models) on backend
- Service Layer abstraction (IUrlService) separates business logic from controllers
- Repository-style use of EF Core via AppDbContext
- Code-First Migrations for schema versioning
- Component-based architecture in React with Hooks (useState/useEffect)
- API-First approach—backend purely offers JSON endpoints consumed by SPA
- Role-based access control enforced server-side
- Single Page Application served either by static assets in wwwroot or via separate Vite dev server.

##### Detail & Clarity
- Essential components: React App, api.js, all Controllers, Service interfaces, DbContext, Entities, Migrations, Database.
- Call out configuration files (appsettings.json) and static assets (wwwroot/lib/*.css/.js) if relevant to deployment.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Layer
    subgraph "Frontend (React SPA)" 
        direction TB
        MainEntry["main.jsx"]:::frontend
        App["App.jsx"]:::frontend
        Login["Login.jsx"]:::frontend
        Registration["Registration.jsx"]:::frontend
        UrlsTable["UrlsTable.jsx"]:::frontend
        UrlInfo["UrlInfo.jsx"]:::frontend
        APIService["api.js"]:::frontend
        Assets["assets"]:::frontend
        ViteConfig["vite.config.js"]:::config
    end

    %% Backend Layer
    subgraph "Backend (ASP.NET MVC)"
        direction TB
        Program["Program.cs"]:::config
        LaunchSettings["launchSettings.json"]:::config
        AppSettings["appsettings.json\nappsettings.Development.json"]:::config
        subgraph "Controllers" 
            direction TB
            AccountController["AccountController.cs"]:::controller
            UrlsController["UrlsController.cs"]:::controller
            HomeController["HomeController.cs"]:::controller
        end
        subgraph "Service Layer"
            direction TB
            IUrlService["IUrlService.cs"]:::service
            UrlServiceClass["UrlServiceClass.cs"]:::service
        end
        subgraph "Data Access (EF Core)"
            direction TB
            AppDbContext["AppDbContext.cs"]:::data
            AppUser["AppUser.cs"]:::data
            ShortUrl["ShortUrl.cs"]:::data
            CreateUrlDto["CreateUrlDto.cs"]:::data
            Seed["Seed.cs"]:::data
            Migrations["Migrations"]:::data
        end
        subgraph "Views"
            direction TB
            HomeViews["Views/Home"]:::view
            SharedViews["Views/Shared"]:::view
        end
        subgraph "Static Assets"
            direction TB
            SiteCSS["site.css\nsite.js"]:::static
            Libs["lib"]:::static
        end
    end

    %% Database Layer
    Database["SQL Server\n(AspNet* tables + ShortUrls)"]:::database

    %% Tests
    Tests["UrlService.Tests"]:::test

    %% Connections
    MainEntry -->|loads| App
    App -->|renders| Login
    App -->|renders| Registration
    App -->|renders| UrlsTable
    App -->|renders| UrlInfo
    App -->|uses| APIService
    Login -->|calls POST /account/login| APIService
    Registration -->|calls POST /account/register| APIService
    UrlsTable -->|calls GET /urls| APIService
    UrlInfo -->|calls GET /urls/:id| APIService
    APIService -->|HTTP| AccountController
    APIService -->|HTTP| UrlsController

    AccountController -->|invokes| IUrlService
    UrlsController -->|invokes| IUrlService
    HomeController -->|serves| HomeViews

    IUrlService -->|implements| UrlServiceClass
    UrlServiceClass -->|uses| AppDbContext
    AppDbContext -->|manages| AppUser
    AppDbContext -->|manages| ShortUrl
    AppDbContext -->|handles| CreateUrlDto
    AppDbContext -->|runs| Seed
    AppDbContext -->|applies| Migrations
    AppDbContext -->|executes| Database

    AccountController -->|auth data| Database
    UrlsController -->|reads/writes| Database

    Program -->|configures| LaunchSettings
    Program -->|loads| AppSettings
    Program -->|serves| StaticAssets
    Program -->|hosts| Backend

    Tests -->|tests| UrlServiceClass

    %% Click Events
    click MainEntry "https://github.com/kirillius7/urlservice/blob/master/frontEnd/UrlService/vite-project/src/main.jsx"
    click App "https://github.com/kirillius7/urlservice/blob/master/frontEnd/UrlService/vite-project/src/App.jsx"
    click Login "https://github.com/kirillius7/urlservice/blob/master/frontEnd/UrlService/vite-project/src/components/Login.jsx"
    click Registration "https://github.com/kirillius7/urlservice/blob/master/frontEnd/UrlService/vite-project/src/components/Registration.jsx"
    click UrlsTable "https://github.com/kirillius7/urlservice/blob/master/frontEnd/UrlService/vite-project/src/components/UrlsTable.jsx"
    click UrlInfo "https://github.com/kirillius7/urlservice/blob/master/frontEnd/UrlService/vite-project/src/components/UrlInfo.jsx"
    click APIService "https://github.com/kirillius7/urlservice/blob/master/frontEnd/UrlService/vite-project/src/service/api.js"
    click Assets "https://github.com/kirillius7/urlservice/tree/master/frontEnd/UrlService/vite-project/src/assets"
    click ViteConfig "https://github.com/kirillius7/urlservice/blob/master/frontEnd/UrlService/vite-project/vite.config.js"
    click Program "https://github.com/kirillius7/urlservice/blob/master/UrlService/Program.cs"
    click LaunchSettings "https://github.com/kirillius7/urlservice/blob/master/UrlService/Properties/launchSettings.json"
    click AccountController "https://github.com/kirillius7/urlservice/blob/master/UrlService/Controllers/AccountController.cs"
    click UrlsController "https://github.com/kirillius7/urlservice/blob/master/UrlService/Controllers/UrlsController.cs"
    click HomeController "https://github.com/kirillius7/urlservice/blob/master/UrlService/Controllers/HomeController.cs"
    click AppDbContext "https://github.com/kirillius7/urlservice/blob/master/UrlService/Models/AppDbContext.cs"
    click AppUser "https://github.com/kirillius7/urlservice/blob/master/UrlService/Models/AppUser.cs"
    click ShortUrl "https://github.com/kirillius7/urlservice/blob/master/UrlService/Models/ShortUrl.cs"
    click CreateUrlDto "https://github.com/kirillius7/urlservice/blob/master/UrlService/Models/CreateUrlDto.cs"
    click Seed "https://github.com/kirillius7/urlservice/blob/master/UrlService/Models/Seed.cs"
    click Migrations "https://github.com/kirillius7/urlservice/tree/master/UrlService/Migrations"
    click IUrlService "https://github.com/kirillius7/urlservice/blob/master/UrlService/Service/IUrlService.cs"
    click UrlServiceClass "https://github.com/kirillius7/urlservice/blob/master/UrlService/Service/UrlServiceClass.cs"
    click HomeViews "https://github.com/kirillius7/urlservice/blob/master/UrlService/Views/Home/Index.cshtml"
    click SharedViews "https://github.com/kirillius7/urlservice/blob/master/UrlService/Views/Shared/_Layout.cshtml"
    click SiteCSS "https://github.com/kirillius7/urlservice/blob/master/UrlService/wwwroot/css/site.css"
    click Libs "https://github.com/kirillius7/urlservice/tree/master/UrlService/wwwroot/lib"
    click Tests "https://github.com/kirillius7/urlservice/blob/master/UrlService.Tests/UrlServiceTests.cs"

    %% Styles
    classDef frontend fill:#9f6,stroke:#3a3,stroke-width:1px
    classDef controller fill:#6cf,stroke:#06a,stroke-width:1px
    classDef service fill:#9cf,stroke:#09a,stroke-width:1px
    classDef data fill:#39f,stroke:#036,stroke-width:1px
    classDef database fill:#f96,stroke:#f60,stroke-width:1px,stroke-dasharray: 5 5
    classDef config fill:#ddd,stroke:#999,stroke-width:1px
    classDef view fill:#ccf,stroke:#66c,stroke-width:1px
    classDef static fill:#fc9,stroke:#c60,stroke-width:1px
    classDef test fill:#c9f,stroke:#60c,stroke-width:1px
```
