Skip to content

Commit

Permalink
add new endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
brash-ram committed Mar 10, 2024
1 parent f65f497 commit ee35c65
Show file tree
Hide file tree
Showing 24 changed files with 331 additions and 63 deletions.
7 changes: 7 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
DB_URL=jdbc:postgresql://localhost:5432/filter
DB_USERNAME=postgres
DB_PASSWORD=qwerty
RABBIT_HOST=localhost
RABBIT_USERNAME=user
RABBIT_PASSWORD=qwerty
RABBIT_PORT=5672
7 changes: 7 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,19 @@ repositories {
mavenCentral()
}

java {
sourceCompatibility = JavaVersion.toVersion("21")
targetCompatibility = JavaVersion.toVersion("21")
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web:3.1.5'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa:3.1.5'
implementation 'org.springframework.boot:spring-boot-starter-validation:3.1.5'
implementation 'org.springframework.boot:spring-boot-starter-amqp:3.1.5'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor:3.1.5'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.3.0'


implementation 'org.postgresql:postgresql:42.5.4'
implementation 'com.google.guava:guava:32.1.3-jre'
Expand Down
43 changes: 23 additions & 20 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@ networks:
backend:

services:
app:
container_name: filter_container
image: filter_image
depends_on:
- rabbitmq
- postgres
build:
context: .
environment:
DB_URL: "jdbc:postgresql://postgres:5432/filter"
DB_USERNAME: "postgres"
DB_PASSWORD: "qwerty"
RABBIT_HOST: "rabbitmq"
RABBIT_USERNAME: "user"
RABBIT_PASSWORD: "qwerty"
RABBIT_PORT: 5672
ports:
- "8080:8080"
networks:
- backend
# app:
# container_name: filter_container
# image: filter_image
# depends_on:
# - rabbitmq
# - postgres
# build:
# context: .
# environment:
# DB_URL: "jdbc:postgresql://postgres:5432/filter"
# DB_USERNAME: "postgres"
# DB_PASSWORD: "qwerty"
# RABBIT_HOST: "rabbitmq"
# RABBIT_USERNAME: "user"
# RABBIT_PASSWORD: "qwerty"
# RABBIT_PORT: 5672
# ports:
# - "8080:8080"
# networks:
# - backend

postgres:
container_name: postgres_container
Expand All @@ -32,6 +32,8 @@ services:
POSTGRES_DB: "filter"
POSTGRES_USER: "postgres"
POSTGRES_PASSWORD: "qwerty"
ports:
- "5432:5432"
volumes:
- ./volumes/2. Init Database:/docker-entrypoint-initdb.d
- ./volumes/postgres-data:/var/lib/postgresql/data
Expand All @@ -56,5 +58,6 @@ services:
- ./volumes/rabbitmq:/var/lib/rabbitmq
ports:
- "15672:15672"
- "5672:5672"
networks:
- backend
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
2 changes: 2 additions & 0 deletions src/main/java/com/brash/config/ApplicationConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand All @@ -15,6 +16,7 @@
*/
@Configuration
@RequiredArgsConstructor
@EnableWebMvc
public class ApplicationConfiguration {

private final RabbitMQConfig rabbitMQConfig;
Expand Down
59 changes: 59 additions & 0 deletions src/main/java/com/brash/controller/FilterController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.brash.controller;

import com.brash.data.entity.Mark;
import com.brash.dto.web.ItemsDTO;
import com.brash.service.MarkService;
import com.brash.util.Mapper;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping(path = "/api/filter")
public class FilterController {

// private final UserService userService;
// private final ItemService itemService;
private final MarkService markService;

@Operation(
summary = "Получить все элементы по убыванию рейтинга оценки для пользователя"
)
@GetMapping("/all")
public ResponseEntity<ItemsDTO> getAll(@NotNull @RequestParam long userId,
@NotNull @RequestParam int offset,
@NotNull @RequestParam int limit) {
List<Mark> marks = markService.getMarks(userId, offset, limit);
return ResponseEntity.ok(Mapper.mapToItemsDTO(marks));
}

@Operation(
summary = "Получить элементы с сгенерированными оценками по убыванию рейтинга для пользователя"
)
@GetMapping("/generated")
public ResponseEntity<ItemsDTO> getAllGeneratedMarks(@NotNull @RequestParam long userId,
@NotNull @RequestParam int offset,
@NotNull @RequestParam int limit) {
List<Mark> marks = markService.getGeneratedMarks(userId, offset, limit);
return ResponseEntity.ok(Mapper.mapToItemsDTO(marks));
}

@Operation(
summary = "Получить схожие элементы с переданным по убыванию рейтинга оценки сходства"
)
@GetMapping("/item")
public ResponseEntity<ItemsDTO> getSimilarityItems(@NotNull @RequestParam long itemId,
@NotNull @RequestParam int offset,
@NotNull @RequestParam int limit) {
List<Mark> marks = markService.getGeneratedMarks(itemId, offset, limit);
return ResponseEntity.ok(Mapper.mapToItemsDTO(marks));
}
}
38 changes: 24 additions & 14 deletions src/main/java/com/brash/controller/MainController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@
import com.brash.dto.web.ItemDTO;
import com.brash.dto.web.MarkDTO;
import com.brash.dto.web.UserDTO;
import com.brash.exception.ExceptionBodyResponse;
import com.brash.exception.NoAvailableMarkException;
import com.brash.service.ItemService;
import com.brash.service.MarkService;
import com.brash.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
Expand All @@ -19,42 +24,47 @@

@RestController
@RequiredArgsConstructor
@RequestMapping(path = "/filter")
@RequestMapping(path = "/api/general")
public class MainController {
private final UserService userService;
private final ItemService itemService;
private final MarkService markService;

@Operation(
summary = "Добавить пользователя"
)
@PostMapping("/add/user")
public ResponseEntity<UserDTO> addUser(@NotNull @Valid @RequestBody UserDTO userDTO) {
userService.addUser(userDTO.id());
return ResponseEntity.ok(userDTO);
}

@Operation(
summary = "Добавить элемент"
)
@PostMapping("/add/item")
public ResponseEntity<ItemDTO> addItem(@NotNull @Valid @RequestBody ItemDTO itemDTO) {
itemService.addItem(itemDTO.id());
return ResponseEntity.ok(itemDTO);
}

@Operation(
summary = "Добавить оценку"
)
@PostMapping("/add/mark")
public ResponseEntity<AddMarkDTO> addMark(@NotNull @Valid @RequestBody AddMarkDTO addMarkDTO) {
markService.addMark(addMarkDTO.mark(), addMarkDTO.userId(), addMarkDTO.itemId());
return ResponseEntity.ok(addMarkDTO);
}

@GetMapping("/get/mark")
public ResponseEntity<MarkDTO> getMark(@NotNull @Valid @RequestParam Long userId,
@NotNull @Valid @RequestParam Long itemId)
throws NoAvailableMarkException {
Mark mark = markService.getMark(userId, itemId);
return ResponseEntity.ok(new MarkDTO(userId, itemId, mark.getMark()));
}
// @Operation(
// summary = "Получить оценку"
// )
// @GetMapping("/mark")
// public ResponseEntity<MarkDTO> getMark(@NotNull @RequestParam long userId,
// @NotNull @RequestParam long itemId) throws NoAvailableMarkException {
// Mark mark = markService.getMark(userId, itemId);
// return ResponseEntity.ok(new MarkDTO(userId, itemId, mark.getMark()));
// }

@GetMapping("/get/mark/all")
public ResponseEntity<List<MarkDTO>> getAllGeneratedMarks(@NotNull @Valid @RequestParam Long userId)
throws NoAvailableMarkException {
List<MarkDTO> marks = markService.getGeneratedMarksDto(userId);
return ResponseEntity.ok(marks);
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/brash/data/entity/Item.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.brash.data.entity;

import com.brash.util.Utils;
import com.brash.util.FilterUtils;
import jakarta.persistence.*;
import lombok.*;
import lombok.experimental.Accessors;
Expand Down Expand Up @@ -47,7 +47,7 @@ public double getAverageMarks() {
if (averageMark == 0.0 && getNotGeneratedMarks().size() != 0) {
synchronized (lock) {
if (averageMark == 0.0) {
averageMark = Utils.getAverageMark(notGeneratedMarks);
averageMark = FilterUtils.getAverageMark(notGeneratedMarks);
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/brash/data/jpa/MarkRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.brash.data.entity.Item;
import com.brash.data.entity.Mark;
import com.brash.data.entity.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
Expand All @@ -13,7 +15,7 @@
@Repository
public interface MarkRepository extends JpaRepository<Mark, Long> {

List<Mark> findAllByIsGenerated(boolean isGenerated);
Page<Mark> findAllByIsGenerated(boolean isGenerated, Pageable pageable);

List<Mark> findAllByMarkLessThan(double value);

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/brash/data/jpa/UserRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.awt.print.Pageable;

@Repository
public interface UserRepository extends JpaRepository<User, Long> {

Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/brash/dto/web/ItemsDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.brash.dto.web;

import jakarta.validation.constraints.NotNull;

import java.util.List;

public record ItemsDTO (
@NotNull List<Long> ids
) {
}
18 changes: 18 additions & 0 deletions src/main/java/com/brash/exception/ControllerAdvice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.brash.exception;

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

@RestControllerAdvice
public class ControllerAdvice {

@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
ExceptionBodyResponse handleExceptions(Exception e) {
return new ExceptionBodyResponse(e.getMessage());
}


}
24 changes: 24 additions & 0 deletions src/main/java/com/brash/exception/ExceptionBodyResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.brash.exception;

import lombok.Data;

import java.util.Date;
import java.util.Map;

@Data
public class ExceptionBodyResponse {
private String message;
private Map<String, String> errors;
private long creationTime;

public ExceptionBodyResponse(String message) {
this.message = message;
this.creationTime = new Date().getTime();
}

public ExceptionBodyResponse(String message, Map<String, String> errors) {
this.message = message;
this.errors = errors;
this.creationTime = new Date().getTime();
}
}
44 changes: 44 additions & 0 deletions src/main/java/com/brash/filter/SimilarityStorage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.brash.filter;

import com.brash.data.entity.Item;
import com.brash.filter.data.ItemNeighbours;
import com.brash.filter.data.SimpleSimilarItems;
import com.brash.filter.data.UserNeighbours;
import com.brash.util.PageableUtils;
import lombok.Getter;
import org.springframework.stereotype.Component;

import java.awt.print.Pageable;
import java.time.LocalDateTime;
import java.util.List;

@Component
public class SimilarityStorage {

@Getter
private ItemNeighbours itemNeighbours;

@Getter
private UserNeighbours userNeighbours;

private LocalDateTime lastUpdate;

public List<SimpleSimilarItems> getNeighbours(Item item) {
return itemNeighbours.neighbours().get(item);
}

public List<SimpleSimilarItems> getNeighbours(Item item, int offset, int limit) {
List<SimpleSimilarItems> similarItems = itemNeighbours.neighbours().get(item);
return PageableUtils.getPage(similarItems, offset, limit);
}

public void setItemNeighbours(ItemNeighbours itemNeighbours) {
this.itemNeighbours = itemNeighbours;
lastUpdate = LocalDateTime.now();
}

public void setUserNeighbours(UserNeighbours userNeighbours) {
this.userNeighbours = userNeighbours;
lastUpdate = LocalDateTime.now();
}
}
Loading

0 comments on commit ee35c65

Please sign in to comment.