Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
9536cde
feat: set up repo structure and backend connection
cbullinger Oct 24, 2025
f55b1d6
feat: add model layer
cbullinger Oct 24, 2025
7e25be6
feat: add CRUD endpoints
cbullinger Oct 24, 2025
dd8fe3a
fix: remove unneeded files and update .gitignore and .gitattributes
cbullinger Oct 24, 2025
7ea6b1e
fix: resolve module access issue with driver v5.6
cbullinger Oct 24, 2025
462d71f
refactor(RuntimeException): Add new DatabaseOperationException.java c…
cbullinger Oct 28, 2025
170c3c5
refactor(pojo): apply patch and use POJO codec
cbullinger Oct 28, 2025
2ea3ff0
refactor(serializer): Apply patch and use ObjectMapper
cbullinger Oct 28, 2025
f454cc8
refactor(field names): implement nested fields consistently
cbullinger Oct 28, 2025
c6eeb46
test: add new tests based on refactors
cbullinger Oct 28, 2025
2d5087b
refactor(DTOs): create missing return types
cbullinger Oct 28, 2025
7b9029c
refactor(spring data): implement spring data fully
cbullinger Oct 28, 2025
360d825
update TODOs
cbullinger Oct 28, 2025
489c950
rename app
cbullinger Oct 28, 2025
5b6f266
remove unnecessary file
cbullinger Oct 28, 2025
605d839
update README
cbullinger Oct 28, 2025
c23c709
Apply Vitaliy feedback
cbullinger Oct 29, 2025
0f3dc54
Apply suggestion from @cbullinger
cbullinger Oct 29, 2025
9253973
Apply suggestion from @cbullinger
cbullinger Oct 29, 2025
cc13e06
docs: update comments throughout for consistency
cbullinger Oct 29, 2025
6155906
docs: update readme and remove internal todo doc
cbullinger Oct 30, 2025
b10f743
fix(movies): add back missing @Document tag
cbullinger Oct 30, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Git Attributes for docs-sample-apps
# Marks auto-generated files that are committed to version control
# so they're collapsed in GitHub PR diffs and excluded from code review

# Maven Wrapper - Auto-generated by Apache Maven (committed to git)
# See: https://maven.apache.org/wrapper/
server/java-spring/mvnw linguist-generated=true
server/java-spring/mvnw.cmd linguist-generated=true
54 changes: 54 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# =============================================================================
# Root .gitignore for MongoDB Sample Apps Monorepo
# =============================================================================
# This file contains patterns common to all projects in the monorepo.
# Backend-specific patterns are defined in each backend's .gitignore file.
# =============================================================================

# Environment Variables (Global)
.env
.env.local
.env.development.local
.env.test.local
.env.production.local
!.env.example

# Operating System Files
.DS_Store
Thumbs.db

# IDE and Editor Files (Global)
.idea/
.vscode/
*.swp
*.swo
*.swn
*.bak
*.tmp
*.iml
.project
.classpath
.settings/
*.sublime-project
*.sublime-workspace

# Logs (Global)
logs/
*.log

# Temporary Files (Global)
*.tmp
*.temp
.cache/

# Node.js (Global - applies to all Node.js projects)
node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.pnpm-debug.log*

# Test Coverage (Global)
coverage/
*.lcov
.nyc_output
3 changes: 2 additions & 1 deletion server/express/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ coverage/
.npm

# macOS
.DS_Store
.DS_Store

13 changes: 13 additions & 0 deletions server/java-spring/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# MongoDB Connection
# Replace with your MongoDB Atlas connection string or local MongoDB URI
MONGODB_URI=mongodb+srv://username:password@cluster.mongodb.net/?retryWrites=true&w=majority

# Server Configuration
# Port on which the Spring Boot application will run
PORT=3001

# CORS Configuration
# Allowed origin for cross-origin requests (frontend URL)
# For multiple origins, separate with commas
CORS_ORIGIN=http://localhost:3000

64 changes: 64 additions & 0 deletions server/java-spring/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Java/Spring Boot - Specific Ignores
# Common patterns
.env
.DS_Store

#Cache
.cache/

# Compiled Files
*.class
*.ctxt
.mtj.tmp/

# Package Files
*.jar
*.war
*.nar
*.ear

# Crash Logs
hs_err_pid*
replay_pid*

# Maven
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
.mvn/wrapper/maven-wrapper.jar

# IntelliJ IDEA
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/

# Eclipse
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

# NetBeans
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

# Spring Boot
spring-boot-devtools.properties
13 changes: 13 additions & 0 deletions server/java-spring/.mvn/wrapper/maven-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Maven Wrapper Configuration
# Use ./mvnw instead of mvn to automatically download and use the specified Maven version.

# Maven Wrapper version (not the Maven version itself)
wrapperVersion=3.3.4

# Download only Maven binaries (recommended for most projects)
distributionType=only-script

# Maven version to download - pins everyone to Maven 3.8.6 for consistent builds
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.6/apache-maven-3.8.6-bin.zip

# Usage: ./mvnw clean install (Unix/macOS) or mvnw.cmd clean install (Windows)
201 changes: 201 additions & 0 deletions server/java-spring/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
# sample-app-java-mflix (INTERNAL)

A Spring Boot REST API demonstrating MongoDB CRUD operations using Spring Data MongoDB with the sample_mflix database.

## Overview

This application provides a REST API for managing movie data from MongoDB's sample_mflix database. It demonstrates:

- Spring Data MongoDB for simplified data access
- CRUD operations (Create, Read, Update, Delete)
- Text search functionality
- Filtering, sorting, and pagination
- Comprehensive error handling
- API documentation with Swagger/OpenAPI
- MongoTemplate for complex queries

## Prerequisites

- Java 21 or later
- Maven 3.6 or later
- MongoDB Atlas account or local MongoDB instance with sample_mflix database

## Project Structure

```
server/java-spring/
├── src/
│ ├── main/
│ │ ├── java/com/mongodb/samplemflix/
│ │ │ ├── SampleMflixApplication.java # Main application class
│ │ │ ├── config/ # Configuration classes
│ │ │ │ ├── MongoConfig.java # MongoDB client configuration
│ │ │ │ ├── CorsConfig.java # CORS configuration
│ │ │ │ └── DatabaseVerification.java # Startup database verification
│ │ │ ├── controller/ # REST controllers
│ │ │ ├── service/ # Business logic layer
│ │ │ ├── repository/ # Data access layer
│ │ │ ├── model/ # Domain models and DTOs
│ │ │ ├── exception/ # Custom exceptions
│ │ │ └── util/ # Utility classes
│ │ └── resources/
│ │ └── application.properties # Application configuration
│ └── test/ # Test classes
├── pom.xml # Maven dependencies
└── README.md
```

## Setup Instructions

### 1. Clone the Repository

```bash
git clone <repository-url>
cd server/java-spring
```

### 2. Configure Environment Variables

Copy the example environment file and update with your MongoDB connection details:

```bash
cp .env.example .env
```

Edit `.env` and set your MongoDB connection string:

```properties
MONGODB_URI=mongodb+srv://username:password@cluster.mongodb.net/?retryWrites=true&w=majority
PORT=3001
CORS_ORIGIN=http://localhost:3000
```

> **Note**: This project uses [spring-dotenv](https://github.com/paulschwarz/spring-dotenv) to automatically load `.env` files, similar to Node.js applications. The `.env` file will be loaded automatically when you run the application.

### 3. Load Sample Data

If you haven't already, load the `sample_mflix` database into your MongoDB instance:

- **MongoDB Atlas**: Use the "Load Sample Dataset" option in your cluster
- **Local MongoDB**: Follow the [MongoDB sample data documentation](https://www.mongodb.com/docs/atlas/sample-data/)

### 4. Build the Project

```bash
mvn clean install
```

### 5. Run the Application

```bash
mvn spring-boot:run
```

The application will start on `http://localhost:3001` (or the port specified in your `.env` file).

## API Documentation

Once the application is running, you can access:

- **Swagger UI**: http://localhost:3001/swagger-ui.html
- **OpenAPI JSON**: http://localhost:3001/api-docs

## API Endpoints

### Movies (✅ Implemented)

- `GET /api/movies` - Get all movies (with filtering, sorting, pagination)
- `GET /api/movies/{id}` - Get a single movie by ID
- `POST /api/movies` - Create a new movie
- `POST /api/movies/batch` - Create multiple movies
- `PUT /api/movies/{id}` - Update a movie
- `PATCH /api/movies` - Update multiple movies
- `DELETE /api/movies/{id}` - Delete a movie
- `DELETE /api/movies` - Delete multiple movies
- `DELETE /api/movies/{id}/find-and-delete` - Find and delete a movie

## Development

### Running Tests

```bash
# Run all tests
mvn test

# Run tests with coverage
mvn test jacoco:report
```

### Building for Production

```bash
mvn clean package
java -jar target/sample-mflix-spring-1.0.0.jar
```

## Implementation Status

### Completed Features

- **Movies CRUD API** - Full create, read, update, delete operations
- **Spring Data MongoDB** - Repository pattern with MongoTemplate for complex queries
- **Text Search** - Full-text search on movie titles, plots, and genres
- **Filtering & Pagination** - Query parameters for filtering, sorting, and pagination
- **Custom Exception Handling** - Global exception handler with proper HTTP status codes
- **Type-Safe DTOs** - Specific response types instead of generic Maps
- **Unit Tests** - 35 tests covering service and controller layers
- **OpenAPI Documentation** - Swagger UI available at `/swagger-ui.html`
- **Database Verification** - Startup checks for database connectivity and indexes


## Technology Stack

- **Framework**: Spring Boot 3.5.7
- **Java Version**: 21
- **MongoDB**: Spring Data MongoDB 4.5.5
- **Build Tool**: Maven
- **API Documentation**: SpringDoc OpenAPI 2.8.13
- **Testing**: JUnit 5, Mockito, Spring Boot Test

## Educational Purpose

This application is designed as an educational sample to demonstrate:

1. How to use Spring Data MongoDB for simplified data access
2. Best practices for Spring Boot REST API development
3. Proper separation of concerns (Controller → Service → Repository)
4. MongoDB CRUD operations and query patterns
5. Error handling and validation in Spring Boot
6. Using MongoTemplate for complex queries alongside Spring Data repositories

## Troubleshooting

### Connection Issues

If you encounter connection issues:

1. Verify your `MONGODB_URI` is correct
2. Check that your IP address is whitelisted in MongoDB Atlas
3. Ensure the sample_mflix database exists and contains data
4. Check the application logs for detailed error messages

### Build Issues

If Maven build fails:

1. Ensure you have Java 21 or later installed: `java -version`
2. Ensure Maven is installed: `mvn -version`
3. Clear Maven cache: `mvn clean`
4. Try rebuilding: `mvn clean install`

## License

[TBD]

## Contributing

[TBD]

## Issues

[TBD]
Loading