~~**Design Specification: Post Creation with Media Claiming in a Modular Monolith (DDD + MediatR)**

---

## 1. Overview
This document describes the design of a feature that allows users to upload media files and later attach them to a Post within a modular monolith application. The system is built using Domain-Driven Design (DDD) principles and uses MediatR for in-process communication between modules.

---

## 2. Modules Involved

### a. Microblogging Module
- Responsible for Post aggregate lifecycle.
- Attaches media to posts based on Media IDs.

### b. MediaManagement Module
- Handles file uploads and MediaUpload entity lifecycle.
- Validates and claims media for Posts.

---

## 3. Key Entities

### Post (Aggregate Root)
- Properties: Id, AuthorId, Content, List<MediaItem>
- Methods: AttachMedia(MediaItem), Create(), etc.

### MediaItem (Owned Entity)
- Properties: MediaId, Uri, Type, AltText, Order

### MediaUpload (Entity in MediaManagement)
- Properties: Id, UploaderId, Uri, MimeType, Status (Unassigned, Attached), PostId (nullable)

---

## 4. Use Case Flow

### Step 1: Upload Media
**Command**: `UploadMediaCommand`
**Handler**: `UploadMediaHandler` (MediaManagement)

- Validates file and stores it.
- Creates MediaUpload record with `Status = Unassigned`.
- Returns `MediaId` to client.

### Step 2: Create Post
**Command**: `CreatePostCommand`
**Handler**: `CreatePostHandler` (Microblogging)

- Creates new Post aggregate.
- For each referenced MediaId:
    - Sends `ClaimMediaRequest` command via MediatR.

### Step 3: Claim Media
**Command**: `ClaimMediaRequest`
**Handler**: `ClaimMediaHandler` (MediaManagement)

- Validates ownership and existence.
- Updates `Status = Attached`, sets `PostId`.
- Publishes `MediaClaimedEvent`.

### Step 4: Attach Media to Post
**Event**: `MediaClaimedEvent`
**Handler**: `MediaClaimedHandler` (Microblogging)

- Loads corresponding Post.
- Calls `post.AttachMedia()`.
- Saves updated Post.

---

## 5. Event & Command Definitions

### UploadMediaCommand
- FileStream, FileName, MimeType, UploaderId

### CreatePostCommand
- AuthorId, Content, List<MediaId>

### ClaimMediaRequest
- MediaId, AuthorId, PostId

### MediaClaimedEvent
- MediaId, PostId, Uri, Type, AltText

---

## 6. Rules and Invariants
- Only the uploader can claim media.
- Media can only be claimed once.
- Posts only accept claimed media.

---

## 7. UML Diagrams

UML Diagrams for the described system are provided below.~~

