Skip to content

Commit

Permalink
Merge pull request #21 from neuefische/20-implement-backend-and-front…
Browse files Browse the repository at this point in the history
…end-logging

20 implement backend and frontend logging
  • Loading branch information
josch87 authored Jun 7, 2024
2 parents c53a5e5 + 40252cb commit 5db908b
Show file tree
Hide file tree
Showing 17 changed files with 302 additions and 30 deletions.
77 changes: 76 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,76 @@
# hh-java-24-2-teamproject-2
# hh-java-24-2-teamproject-2

## Introduction
RestaurantApp is an application for managing and organizing restaurant details. Users can save their favorite restaurants information.

## Prerequisites

The following technologies should be installed on the system:

- Java SDK version 22
- Node.js
- NPM (Node Package Manager)
- optional: IntelliJ IDEA Ultimate Edition

## Dependencies

### Backend
- spring-boot-starter-data-mongodb
- spring-boot-starter-parent Version 3.3.0
- spring-boot-starter-web
- spring-boot-starter-validation
- logback-logtail Version 0.3.4 from com.logtail
- de.flapdoodle.embed.mongo.spring3x Version 4.13.0 from de.flapdoodle.embed (only Test-Scope)
- spring-boot-starter-test (only Test-Scope)
- spring-boot-starter-actuator

### Frontend

- TypeScript
- React
- Jest
- Babel
- Axios
- Test-Library/React
- Styled-Components
- ESLint

## Getting Started

Follow these steps to get the project up and running on your local machine:

1. Clone the repository:
`git clone https://github.com/neuefische/hh-java-24-2-teamproject-2.git`

2. Navigate to the project's root directory:
`cd hh-java-24-2-teamproject-2/`

3. Setup all environment variables
1. Backend: Copy application-development-properties.sample to application-development-properties and replace with your secrets:
`cp backend/src/main/resources/application-development.properties.sample backend/src/main/resources/application-development.properties`
2. Frontend: Copy .env.local.sample to .env.local and replace with your secrets:
`cp frontend/.env.local.sample frontend/.env.local `

4. Navigate to the frontend's root directory:
`cd frontend/`

5. Install the NPM dependencies:
`npm install`

6. Start the project:
For IntelliJ IDEA the configuration for all runs should be automatically loaded using the files in `.run/` folder.
- For the backend, you might want to execute the main Spring Boot Application class.
- For the frontend, typically you would use `npm dev`.

## Running Tests
For IntelliJ IDEA the configuration for all test runs should be automatically loaded using the files in `.run/` folder.
- For the backend, you might want to execute the test folder.
- For the frontend, typically you would use `npm test`.

## Authors / Acknowledgments
- [Samuel Gesang](https://github.com/gcode-de)
- [Jonas Honecker](https://github.com/jonashonecker)
- [Daniel Pohl](https://github.com/daniel-pohl)
- [Aljoscha Zöller](https://github.com/josch87)

[See all git contributors](https://github.com/neuefische/hh-java-24-2-teamproject-2/graphs/contributors)
17 changes: 12 additions & 5 deletions backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>com.logtail</groupId>
<artifactId>logback-logtail</artifactId>
<version>0.3.4</version>
</dependency>
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo.spring3x</artifactId>
Expand All @@ -36,14 +45,12 @@
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.neuefische.team2.backend.restaurant.domain.NewRestaurantDTO;
import com.neuefische.team2.backend.exceptions.ResourceNotFoundException;
import com.neuefische.team2.backend.restaurant.domain.NewRestaurantDTO;
import com.neuefische.team2.backend.restaurant.domain.Restaurant;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -11,8 +10,6 @@
import org.springframework.web.bind.annotation.RestController;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import com.neuefische.team2.backend.restaurant.domain.NewRestaurantDTO;
import com.neuefische.team2.backend.exceptions.NoSuchRestaurantException;
import com.neuefische.team2.backend.restaurant.domain.Restaurant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class RestaurantService {
Logger logger = LoggerFactory.getLogger(RestaurantService.class);

private final RestaurantRepository restaurantRepository;

Expand All @@ -23,20 +25,37 @@ public List<Restaurant> getRestaurants() {
}

public Restaurant findRestaurantById(String id) {
return restaurantRepository.findById(id).orElseThrow(() -> new NoSuchRestaurantException("Restaurant with id " + id + " not found"));
logger.info("Trying to find restaurant with ID {}", id);

Restaurant restaurant = restaurantRepository.findById(id)
.orElseThrow(() -> {
logger.error("Could not find restaurant with ID {}", id);
return new NoSuchRestaurantException("Restaurant with id " + id + " not found");
});

logger.info("Found restaurant with ID {}", id);

return restaurant;
}

public Restaurant addRestaurant(Restaurant restaurant) {
return restaurantRepository.save(restaurant);
}

public Restaurant updateRestaurant(NewRestaurantDTO updatedRestaurantDTO, String id) throws ResourceNotFoundException {
Optional<Restaurant> existingRestaurant = restaurantRepository.findById(id);
if (existingRestaurant.isPresent()) {
Restaurant updatedRestaurant = new Restaurant(id, updatedRestaurantDTO.title().trim(), updatedRestaurantDTO.city().trim());
return restaurantRepository.save(updatedRestaurant);
} else {
throw new ResourceNotFoundException("Restaurant not found with id " + id);
}
logger.info("Trying to update restaurant with ID {}", id);

this.findRestaurantById(id);
Restaurant updatedRestaurant = new Restaurant(
id,
updatedRestaurantDTO.title().trim(),
updatedRestaurantDTO.city().trim()
);
Restaurant savedRestaurant = restaurantRepository.save(updatedRestaurant);

logger.info("Updated restaurant with ID {}", id);
return savedRestaurant;
}


}
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# Database Configuration
MONGODB_URI=your_db_uri
MONGODB_URI=your_db_uri

# Logging Configuration
BETTERSTACK_TOKEN=your_betterstack_token
8 changes: 7 additions & 1 deletion backend/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
spring.application.name=backend

# Database
spring.data.mongodb.uri=${MONGODB_URI}
spring.data.mongodb.database=restaurantApp
spring.data.mongodb.database=restaurantApp

# Logging
logging.level.com.neuefische.team2.backend=info
management.endpoints.web.exposure.include=loggers
19 changes: 19 additions & 0 deletions backend/src/main/resources/logback-spring.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<springProperty scope="context" name="betterStackToken" source="BETTERSTACK_TOKEN"/>
<appender name="Logtail" class="com.logtail.logback.LogtailAppender">
<appName>RestaurantApp Backend</appName>
<sourceToken>${betterStackToken}</sourceToken>
<mdcFields>requestId,requestTime</mdcFields>
<mdcTypes>string,int</mdcTypes>
</appender>
<appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="Logtail" />
<appender-ref ref="Console" />
</root>
</configuration>
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.neuefische.team2.backend.restaurant;

import com.neuefische.team2.backend.exceptions.ResourceNotFoundException;
import com.neuefische.team2.backend.restaurant.domain.NewRestaurantDTO;
import com.neuefische.team2.backend.exceptions.NoSuchRestaurantException;
import com.neuefische.team2.backend.restaurant.domain.Restaurant;
Expand Down Expand Up @@ -73,13 +72,13 @@ void updateRestaurant_whenRestaurantDoesNotExist_thenThrowResourceNotFoundExcept
when(mockRestaurantRepository.findById("1")).thenReturn(Optional.empty());

// WHEN / THEN
assertThrows(ResourceNotFoundException.class, () -> {
restaurantService.updateRestaurant(updatedRestaurantData, "1");
});
assertThrows(NoSuchRestaurantException.class, () ->
restaurantService.updateRestaurant(updatedRestaurantData, "1"));

verify(mockRestaurantRepository).findById("1");
verify(mockRestaurantRepository, never()).save(any(Restaurant.class));
}

@Test
void findRestaurantById_whenRestaurantExists_thenReturnRestaurant() {
//GIVEN
Expand Down
2 changes: 2 additions & 0 deletions frontend/.env.local.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Logging
VITE_BETTERSTACK_TOKEN=your_betterstack_token
95 changes: 94 additions & 1 deletion frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5db908b

Please sign in to comment.