Files
pos-system/services/social-service-net/docs/vi

Social Service - Dịch Vụ Quản Lý Quan Hệ Xã Hội

Microservice .NET 10 quản lý quan hệ xã hội giữa users: kết bạn, theo dõi và block.

Tổng Quan

Social Service là microservice quản lý tất cả các mối quan hệ xã hội giữa users trong GoodGo Platform:

  • Kết bạn (Friendship) - Gửi/chấp nhận/từ chối yêu cầu kết bạn, hủy kết bạn
  • Theo dõi (Following) - Follow/unfollow users
  • Block (User Blocking) - Block/unblock users với lý do tùy chọn
  • User Profile - Đồng bộ thông tin user từ IAM Service

Kiến Trúc & Patterns

Pattern Triển khai
DDD Aggregates, Entities, Value Objects, Domain Events
CQRS Commands/Queries tách biệt với MediatR
Clean Architecture 3-layer: Domain, Infrastructure, API
EF Core 10 PostgreSQL với connection resilience
Structured Logging Serilog với console và Seq

Yêu Cầu

Yêu cầu Phiên bản
.NET SDK 10.0.101+
Docker 24.0+
PostgreSQL 15+

Bắt Đầu Nhanh

Chạy với Docker

# Khởi động tất cả services (API + PostgreSQL + Redis)
docker-compose up -d

# Xem logs
docker-compose logs -f socialservice-api

Chạy Local

# Khôi phục dependencies
dotnet restore

# Build tất cả projects
dotnet build

# Chạy API
dotnet run --project src/SocialService.API

Domain Model

Aggregates

1. Relationship Aggregate

Quản lý quan hệ kết bạn và theo dõi giữa users.

classDiagram
    class Relationship {
        +Guid Id
        +Guid RequesterId
        +Guid AddresseeId
        +RelationshipType Type
        +RelationshipStatus Status
        +DateTime CreatedAt
        +DateTime? UpdatedAt
        +Accept()
        +Reject()
        +Cancel()
        +Remove()
    }
    
    class RelationshipType {
        <<Enumeration>>
        Friendship
        Following
    }
    
    class RelationshipStatus {
        <<Enumeration>>
        Pending
        Accepted
        Rejected
        Cancelled
    }
    
    Relationship --> RelationshipType
    Relationship --> RelationshipStatus

Business Rules:

  • Following tự động được chấp nhận
  • Friendship yêu cầu xác nhận từ người nhận
  • Không thể tạo quan hệ với chính mình
  • Chỉ Pending requests mới có thể Accept/Reject/Cancel
  • Chỉ Accepted relationships mới có thể Remove

2. UserBlock Aggregate

Quản lý việc block users.

Field Mô tả
BlockerId ID user thực hiện block
BlockedId ID user bị block
Reason Lý do block (tùy chọn)
CreatedAt Thời gian block

3. UserProfile Aggregate

Cache thông tin user được đồng bộ từ IAM Service.

Field Mô tả
UserId ID user từ IAM
DisplayName Tên hiển thị
AvatarUrl URL avatar
Bio Tiểu sử
LastSyncedAt Thời gian sync cuối

API Endpoints

Relationships - Kết Bạn

Method Endpoint Mô Tả
GET /api/v1/relationships/users/{userId}/friends Lấy danh sách bạn bè
POST /api/v1/relationships/friend-requests Gửi yêu cầu kết bạn
PUT /api/v1/relationships/friend-requests/{id} Phản hồi yêu cầu (accept/reject)
GET /api/v1/relationships/users/{id1}/mutual-friends/{id2} Lấy bạn chung
GET /api/v1/relationships/users/{userId}/suggestions Gợi ý kết bạn

Relationships - Theo Dõi

Method Endpoint Mô Tả
POST /api/v1/relationships/follow Theo dõi user
DELETE /api/v1/relationships/follow Bỏ theo dõi user

Block Users

Method Endpoint Mô Tả
POST /api/v1/blocks Block user
DELETE /api/v1/blocks Unblock user
GET /api/v1/blocks/users/{userId} Lấy danh sách blocked users

Admin Backoffice

Method Endpoint Mô Tả
GET /api/v1/admin/social/relationships Lấy tất cả relationships (filter, phân trang)
GET /api/v1/admin/social/relationships/{id} Chi tiết relationship
DELETE /api/v1/admin/social/relationships/{id} Admin xóa relationship
GET /api/v1/admin/social/blocks Lấy tất cả blocks (phân trang)
DELETE /api/v1/admin/social/blocks/{id} Admin xóa block
GET /api/v1/admin/social/statistics Thống kê tổng quan

Health Endpoints

Endpoint Mục Đích
/health Trạng thái health đầy đủ
/health/live Kiểm tra sống
/health/ready Kiểm tra sẵn sàng

Cấu Trúc Dự Án

social-service-net/
├── src/
│   ├── SocialService.API/
│   │   ├── Controllers/
│   │   │   ├── RelationshipsController.cs   # Friends + Following APIs
│   │   │   └── BlocksController.cs          # Block/Unblock APIs
│   │   ├── Application/
│   │   │   ├── Commands/                    # 6 commands
│   │   │   ├── Queries/                     # 4 queries
│   │   │   ├── Behaviors/                   # MediatR pipeline
│   │   │   └── Validations/                 # FluentValidation
│   │   └── Program.cs
│   │
│   ├── SocialService.Domain/
│   │   ├── AggregatesModel/
│   │   │   ├── RelationshipAggregate/       # Relationship, Status, Type
│   │   │   ├── UserBlockAggregate/          # UserBlock
│   │   │   └── UserProfileAggregate/        # UserProfile (IAM sync)
│   │   ├── Events/                          # 7 domain events
│   │   ├── Exceptions/                      # SocialDomainException
│   │   └── SeedWork/                        # Base classes
│   │
│   └── SocialService.Infrastructure/
│       ├── EntityConfigurations/            # EF Core mappings
│       ├── Repositories/                    # 3 repositories
│       ├── Idempotency/                     # Request deduplication
│       └── SocialServiceContext.cs          # DbContext
│
├── tests/
│   ├── SocialService.UnitTests/
│   └── SocialService.FunctionalTests/
│
├── Dockerfile
├── docker-compose.yml
└── docs/

CQRS Pattern

Commands (Thao Tác Ghi)

Command Mô Tả
SendFriendRequestCommand Gửi yêu cầu kết bạn
RespondToFriendRequestCommand Accept/Reject friend request
FollowUserCommand Theo dõi user
UnfollowUserCommand Bỏ theo dõi
BlockUserCommand Block user với lý do tùy chọn
UnblockUserCommand Unblock user

Queries (Thao Tác Đọc)

Query Mô Tả
GetFriendsQuery Danh sách bạn bè với phân trang
GetMutualFriendsQuery Bạn chung giữa 2 users
GetFriendSuggestionsQuery Gợi ý kết bạn
GetBlockedUsersQuery Danh sách users bị block

Domain Events

Event Trigger
FriendRequestSentDomainEvent Khi gửi friend request
FriendshipCreatedDomainEvent Khi friend request được accept
UserFollowedDomainEvent Khi follow user
RelationshipStatusChangedDomainEvent Khi status thay đổi
RelationshipRemovedDomainEvent Khi unfriend/unfollow
UserBlockedDomainEvent Khi block user
UserUnblockedDomainEvent Khi unblock user

Cấu Hình

Biến Môi Trường

Biến Mô Tả Mặc định
ASPNETCORE_ENVIRONMENT Tên môi trường Development
DATABASE_URL PostgreSQL connection string -
REDIS_URL Redis connection string -

appsettings.json

{
  "ConnectionStrings": {
    "DefaultConnection": "Host=localhost;Database=socialservice;Username=postgres;Password=postgres"
  },
  "Serilog": {
    "MinimumLevel": "Information"
  }
}

Kiểm Thử

# Chạy tất cả tests
dotnet test

# Chạy với coverage
dotnet test /p:CollectCoverage=true

# Chạy project test cụ thể
dotnet test tests/SocialService.UnitTests

Triển Khai

Docker Build

# Build Docker image
docker build -t socialservice:latest .

# Chạy container
docker run -p 5000:8080 --env-file .env socialservice:latest

Tài Nguyên

Giấy Phép

Độc quyền - GoodGo Platform