Skip to content

Den1s-coder/MySocialNetwork

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

.NET Entity Framework Core SignalR

🌐 Social Network Server

Backend part of a social network built on .NET 8 using Clean Architecture principles.

πŸ“‹ Table of Contents

🎯 Project Description

This project is the backend part of a social network that provides APIs for managing users, posts, comments, and real-time chat. The system is built using Clean Architecture, ensuring flexibility, testability, and ease of code maintenance.

πŸ—οΈ Architecture

The project uses Clean Architecture with the following layers:

πŸ“ SocialNetwork.API (Presentation Layer)
β”œβ”€β”€ Controllers/ - API Controllers
β”œβ”€β”€ Hubs/ - SignalR Hubs for real-time communication
β”œβ”€β”€ Middleware/ - Custom middleware
└── Extensions/ - Configuration extensions

πŸ“ SocialNetwork.Application (Application Layer)
β”œβ”€β”€ DTO/ - Data Transfer Objects
β”œβ”€β”€ Interfaces/ - Service contracts
β”œβ”€β”€ Service/ - Business logic
└── Mappings/ - AutoMapper profiles

πŸ“ SocialNetwork.Domain (Domain Layer)
β”œβ”€β”€ Entities/ - Domain models
β”œβ”€β”€ Enums/ - Enumerations
└── Interfaces/ - Domain contracts

πŸ“ SocialNetwork.Infrastructure (Infrastructure Layer)
β”œβ”€β”€ Repos/ - Repositories
β”œβ”€β”€ Configurations/ - EF Core configurations
β”œβ”€β”€ Security/ - JWT providers
└── SocialDbContext.cs - Database context

πŸ› οΈ Technologies

  • .NET 8 - Main framework
  • Entity Framework Core 9.0 - ORM for database operations
  • SQL Server - Database
  • SignalR - Real-time communication
  • JWT - Authentication and authorization
  • AutoMapper - Object mapping
  • Swagger/OpenAPI - API documentation
  • xUnit - Testing

⚑ Features

πŸ” Authentication & Users

  • User registration
  • Login with JWT tokens
  • User profile management
  • User ban system

πŸ“ Posts & Comments

  • Create, view and edit posts
  • Comment on posts
  • Post ban system
  • Display all posts and comments

πŸ’¬ Real-time Chat

  • Private chats between users
  • Group chats
  • Channels
  • Real-time messaging via SignalR

πŸ”§ Administrative Functions

  • User and post banning
  • Access rights management

πŸš€ Installation & Setup

Prerequisites

  • .NET 8 SDK
  • SQL Server (local or Azure)
  • Visual Studio 2022 or VS Code

Installation Steps

  1. Clone the repository
git clone <repository-url>
cd SocialNetwork_Server
  1. Install dependencies
dotnet restore
  1. Configure database

    • Update connection string in appsettings.json:
    "ConnectionStrings": {
      "DefaultConnection": "Data Source=YOUR_SERVER;Initial Catalog=SocialNetworkDb;Integrated Security=True;TrustServerCertificate=True"
    }
  2. Apply migrations

dotnet ef database update --project SocialNetwork.Infrastructure --startup-project SocialNetwork.API
  1. Run the application
dotnet run --project SocialNetwork.API

The application will be available at: https://localhost:7000

πŸ“š API Documentation

After running the application, Swagger documentation is available at:

  • Swagger UI: https://localhost:7000/swagger

Main Endpoints:

Authentication

  • POST /api/auth/register - User registration
  • POST /api/auth/login - User login

Posts

  • GET /api/post - Get all posts
  • GET /api/post/{id} - Get post by ID
  • POST /api/post - Create new post (requires authentication)
  • DELETE /api/post/{id} - Ban post (admin only)

Users

  • GET /api/user - Get all users
  • GET /api/user/{id} - Get user by ID

Comments

  • GET /api/comment - Get all comments
  • POST /api/comment - Create comment (requires authentication)

Real-time Chat

  • POST /chatHub - SignalR hub for real-time communication

πŸ“ Project Structure

SocialNetwork_Server/
β”œβ”€β”€ SocialNetwork.API/                 # Web API project
β”‚   β”œβ”€β”€ Controllers/                   # API Controllers
β”‚   β”œβ”€β”€ Hubs/                         # SignalR Hubs
β”‚   β”œβ”€β”€ Middleware/                   # Custom middleware
β”‚   β”œβ”€β”€ Extensions/                   # Extensions
β”‚   └── Program.cs                    # Entry point
β”œβ”€β”€ SocialNetwork.Application/         # Business logic
β”‚   β”œβ”€β”€ DTO/                         # Data Transfer Objects
β”‚   β”œβ”€β”€ Interfaces/                  # Service contracts
β”‚   β”œβ”€β”€ Service/                     # Service implementations
β”‚   └── Mappings/                    # AutoMapper profiles
β”œβ”€β”€ SocialNetwork.Domain/             # Domain layer
β”‚   β”œβ”€β”€ Entities/                    # Domain models
β”‚   β”œβ”€β”€ Enums/                       # Enumerations
β”‚   └── Interfaces/                  # Domain contracts
β”œβ”€β”€ SocialNetwork.Infrastructure/     # Infrastructure layer
β”‚   β”œβ”€β”€ Repos/                       # Repositories
β”‚   β”œβ”€β”€ Configurations/              # EF Core configurations
β”‚   β”œβ”€β”€ Security/                    # JWT providers
β”‚   └── SocialDbContext.cs           # Database context
└── SocialNetwork.Tests/             # Tests

πŸ—„οΈ Database

Main Entities:

  • User - System users
  • Post - User posts
  • Comment - Post comments
  • Chat - Chats (private, group, channels)
  • Message - Chat messages
  • UserChat - User-Chat relationship

Migrations:

The project uses Entity Framework Core migrations for database schema management. All migrations are located in the SocialNetwork.Infrastructure/Migrations/ folder.

πŸ” Authentication

The system uses JWT (JSON Web Tokens) for authorization:

  • Secret Key: Configured in appsettings.json
  • Expiration: 12 hours (configurable)
  • Claims: User ID, Email, Role

πŸ’¬ Real-time Chat

The chat system is built on SignalR and supports:

  • Private chats - one-on-one
  • Group chats - multiple users
  • Channels - public channels

Connecting to Chat:

const connection = new signalR.HubConnectionBuilder()
    .withUrl("/chatHub")
    .build();

// Join chat
await connection.invoke("JoinChat", chatId, userId);

// Send message
await connection.invoke("SendMessage", chatId, "Hello!");

Logging

The project uses the built-in .NET logging system with different levels:

  • Information - General information
  • Warning - Warnings
  • Error - Errors

πŸ“ License

This project is developed for educational purposes.