Skip to content
This repository was archived by the owner on Dec 8, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions .idea/compiler.xml

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

4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ tasks.register('composeStart') {
errorOutput = standardOutput
}

def hosts = ['localhost', 'localhost', 'localhost']
def ports = [5433, 5434, 5435]
def hosts = ['148.32.2.3', '148.32.2.4', '148.32.2.5']
def ports = [5432, 5432, 5432]
def timeoutSeconds = 60
def intervalMillis = 1000
def start = System.currentTimeMillis()
Expand Down
22 changes: 0 additions & 22 deletions compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ services:
environment:
- POSTGRES_USER=root
- POSTGRES_PASSWORD=password
ports:
- 5433:5433
volumes:
- db-user:/var/lib/postgresql/data
networks:
Expand All @@ -18,8 +16,6 @@ services:
environment:
- POSTGRES_USER=root
- POSTGRES_PASSWORD=password
ports:
- 5434:5434
volumes:
- db-orders:/var/lib/postgresql/data
networks:
Expand All @@ -31,34 +27,16 @@ services:
environment:
- POSTGRES_USER=root
- POSTGRES_PASSWORD=password
ports:
- 5435:5435
volumes:
- db-restaurant:/var/lib/postgresql/data
networks:
restaurant:
ipv4_address: 148.32.2.5

pgadmin4:
container_name: pgadmin4
image: dpage/pgadmin4
environment:
- PGADMIN_DEFAULT_EMAIL=root@root.com
- PGADMIN_DEFAULT_PASSWORD=passwd
ports:
- 80:80
volumes:
- pgadmin4-volume:/var/lib/pgadmin
networks:
restaurant:
ipv4_address: 148.32.2.2

volumes:
app:
db-user:
db-restaurant:
db-orders:
pgadmin4-volume:

networks:
restaurant:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.thegethuber.restaurantservice.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
@EnableWebSecurity
public class SecurityConfig {
// DEBUG
// TODO: Properly setup authorization system. NOT BYPASS ROLES
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception{
return httpSecurity
.csrf(customizer -> customizer.disable())
.authorizeHttpRequests(req -> req.anyRequest().authenticated())
.httpBasic(Customizer.withDefaults())
.sessionManagement(
session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.thegethuber.restaurantservice.controllers;

import com.thegethuber.restaurantservice.dto.DishRequestDto;
import com.thegethuber.restaurantservice.dto.DishResponseDto;
import com.thegethuber.restaurantservice.dto.RestaurantRequestDto;
import com.thegethuber.restaurantservice.dto.RestaurantResponseDto;
import com.thegethuber.restaurantservice.services.RestaurantService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

Expand All @@ -17,17 +18,101 @@
public class RestaurantController {
private final RestaurantService restaurantService;

//region<GET MAPPERS>

// get all restaurants
@GetMapping("/list")
public ResponseEntity<List<RestaurantResponseDto>> listRestaurants(){
return ResponseEntity.ok(
restaurantService.listRestaurants()
);
}

// get restaurant by id
@GetMapping("/{id}")
public ResponseEntity<RestaurantResponseDto> getRestaurant(@PathVariable Long id){
return ResponseEntity.ok(
restaurantService.getRestaurant(id)
restaurantService.getRestaurant(id)
);
}
// get all dishes from restaurant by id
@GetMapping("/{restaurantId}/dishes")
public ResponseEntity<List<DishResponseDto>> listDishes(@PathVariable Long restaurantId){
return ResponseEntity.ok(
restaurantService.listDishesByRestaurant(restaurantId)
);
}
// get all dishes
@GetMapping("/dishes")
public ResponseEntity<List<DishResponseDto>> listDishes(){
return ResponseEntity.ok(
restaurantService.listDishes()
);
}
// get dish by id
@GetMapping("/dish/{dishId}")
public ResponseEntity<DishResponseDto> getDish(@PathVariable Long dishId){
return ResponseEntity.ok(
restaurantService.getDish(dishId)
);
}
// get dish by id from restaurant by id
@GetMapping("/{restaurantId}/dish/{dishId}")
public ResponseEntity<DishResponseDto> getDish(@PathVariable Long restaurantId, @PathVariable Long dishId){
return ResponseEntity.ok(
restaurantService.getDish(dishId)
);
}

//endregion

//region<POST MAPPERS>

// create restaurant
@PostMapping
@PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<RestaurantResponseDto> createRestaurant(@RequestBody RestaurantRequestDto restaurantRequestDto) {
return ResponseEntity.ok(restaurantService.createRestaurant(restaurantRequestDto));
}
// add dish to restaurant by id
@PostMapping("/{id}/dishes")
@PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<DishResponseDto> addDish(@PathVariable Long id, @RequestBody DishRequestDto dishRequestDto) {
return ResponseEntity.ok(restaurantService.addDish(dishRequestDto));
}
//endregion

//region <PUT MAPPERS>

// update dish by id from restaurant by id
@PutMapping("/{id}/dishes/{dishId}")
@PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<DishResponseDto> updateDish(@PathVariable Long id, @PathVariable Long dishId, @RequestBody DishRequestDto dishRequestDto) {
return ResponseEntity.ok(restaurantService.updateDish(dishId, dishRequestDto));
}
// update restaurant
@PutMapping("/{id}")
@PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<RestaurantResponseDto> updateRestaurant(@PathVariable Long id, @RequestBody RestaurantRequestDto restaurantRequestDto) {
return ResponseEntity.ok(restaurantService.updateRestaurant(id, restaurantRequestDto));
}
//endregion

//region<DELETE MAPPERS>

// delete restaurant by id
@DeleteMapping("/{id}")
@PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<Void> deleteRestaurant(@PathVariable Long id) {
restaurantService.deleteRestaurant(id);
return ResponseEntity.noContent().build();
}

// delete dish by id from restaurant by id
@DeleteMapping("/{id}/dishes/{dishId}")
@PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<Void> deleteDish(@PathVariable Long id, @PathVariable Long dishId) {
restaurantService.deleteDish(dishId);
return ResponseEntity.noContent().build();
}
//endregion
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.thegethuber.restaurantservice.exceptions;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(value = HttpStatus.NOT_FOUND)
public class DishDoesNotExistsException extends RuntimeException{
public DishDoesNotExistsException(String message){
super(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@

import com.thegethuber.restaurantservice.entities.Dish;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.CrudRepository;

import java.util.Collection;

public interface DishRepository extends JpaRepository<Dish, Long> {
void removeDishById(Long id);

Dish getDishById(Long id);

Collection<Dish> findAllByRestaurantId(Long restaurantId);

<T> CrudRepository<Dish, Long> findByRestaurantId(Long restaurantId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ public interface RestaurantService {
DishResponseDto updateDish(Long dishId, DishRequestDto dishRequestDto);
void deleteDish(Long dishId);
DishResponseDto getDish(Long dishId);
List<DishResponseDto> listDishesByRestaurant(Long restaurantId);
List<DishResponseDto> listDishes();
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,24 @@ public void deleteDish(Long dishId){

dishRepository.removeDishById(dishId);
}
@Override
public DishResponseDto getDish(Long dishId){
Dish dish = dishRepository.findById(dishId)
.orElseThrow(() -> new DishDoesNotExistsException("Dish with this ID (" + dishId + ") does not exists!"));
return dishMapper.toResponseDto(dish);
}
@Override
public List<DishResponseDto> listDishesByRestaurant(Long restaurantId){
return dishRepository.findAllByRestaurantId(restaurantId)
.stream()
.map(dishMapper::toResponseDto)
.collect(Collectors.toList());
}
@Override
public List<DishResponseDto> listDishes(){
return dishRepository.findAll()
.stream()
.map(dishMapper::toResponseDto)
.collect(Collectors.toList());
}
}
3 changes: 2 additions & 1 deletion restaurantservice/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=none

spring.security.user.name=user
spring.security.user.password=strongpassword
spring.security.user.password=strongpassword
spring.security.user.roles=ADMIN