Skip to content

Commit

Permalink
Merge pull request #1096 from bounswe/main
Browse files Browse the repository at this point in the history
deploying last changes
  • Loading branch information
zeynep-baydemir authored Dec 24, 2023
2 parents 0788616 + c5c5e20 commit 82f9690
Show file tree
Hide file tree
Showing 69 changed files with 3,706 additions and 351 deletions.
10 changes: 5 additions & 5 deletions app/annotation/annotation/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM eclipse-temurin:17-jdk-alpine
WORKDIR /app
VOLUME /app/images
COPY target/*.jar /app/bounswe.jar
FROM maven:3.9.6-eclipse-temurin-21-jammy
WORKDIR /app/annotation
COPY . /app/annotation
RUN mvn clean install
EXPOSE 8080
ENTRYPOINT ["java","-jar","/app/bounswe.jar"]
ENTRYPOINT ["java","-jar","/app/annotation/target/annotation-1.0-SNAPSHOT.jar"]
9 changes: 9 additions & 0 deletions app/annotation/annotation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,13 @@
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,13 @@ public ResponseEntity<List<Map<String, Object>>> getAnnotation(@RequestParam Str
}
return ResponseEntity.ok(annotations);
}

@GetMapping("/get-image-annotations")
public ResponseEntity<List<Map<String, Object>>> getImageAnnotations(@RequestParam String source) {
List<Map<String, Object>> annotations = annotationService.getImageAnnotations(source);
if(annotations == null || annotations.isEmpty()) {
return ResponseEntity.notFound().build();
}
return ResponseEntity.ok(annotations);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.app.annotation.controller;

import com.app.annotation.exception.BadRequestException;
import com.app.annotation.exception.ResourceNotFoundException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;

@ControllerAdvice
public class CustomExceptionHandler {
@ExceptionHandler(ResourceNotFoundException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ResponseEntity<Object> handleResourceNotFoundException(ResourceNotFoundException ex) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ex.getMessage());
}

@ExceptionHandler(BadRequestException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ResponseEntity<Object> handleBadRequestException(BadRequestException ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ex.getMessage());
}

@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ResponseEntity<String> handleValidationException(MethodArgumentNotValidException ex) {
String defaultMessage = ex.getBindingResult()
.getFieldError()
.getDefaultMessage();
return ResponseEntity.badRequest().body(defaultMessage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ public class SelectorDto {

@Positive
private Integer end;

private String value;

@Pattern(regexp = "^(https?|ftp)://[^\\s/$.?#].\\S*$", message = "Invalid URL")
private String conformsTo;
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.app.annotation.dto.request;


import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonSetter;
import jakarta.validation.constraints.NotEmpty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
@Data
@AllArgsConstructor
Expand All @@ -24,5 +29,46 @@ public class TargetDto {
private String source;

@NotEmpty(message = "target selector can not be empty.")
@JsonIgnore
private List<SelectorDto> selector;

@JsonSetter("selector")
public void setSelector(Object selector) {
if (selector instanceof List) {
List<LinkedHashMap<String, Object>> hashList;
hashList = (List<LinkedHashMap<String, Object>>) selector;
this.selector = mapToSelectorDtoList(hashList);
} else {
LinkedHashMap<String, Object> hash = (LinkedHashMap<String, Object>) selector;
this.selector = Collections.singletonList(convertToSelectorDto(hash));
}
}

public static List<SelectorDto> mapToSelectorDtoList(List<LinkedHashMap<String, Object>> inputList) {
List<SelectorDto> resultList = new ArrayList<>();

if (inputList != null) {
for (LinkedHashMap<String, Object> map : inputList) {
SelectorDto selectorDto = convertToSelectorDto(map);
resultList.add(selectorDto);
}
}

return resultList;
}

private static SelectorDto convertToSelectorDto(LinkedHashMap<String, Object> map) {
SelectorDto selectorDto = new SelectorDto();
selectorDto.setType((String) map.get("type"));
selectorDto.setExact((String) map.get("exact"));
selectorDto.setPrefix((String) map.get("prefix"));
selectorDto.setSuffix((String) map.get("suffix"));
selectorDto.setStart((Integer) map.get("start"));
selectorDto.setEnd((Integer) map.get("end"));
selectorDto.setValue((String) map.get("value"));
selectorDto.setConformsTo((String) map.get("conformsTo"));
// Add other fields as needed

return selectorDto;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.app.annotation.exception;

public class BadRequestException extends RuntimeException {
public BadRequestException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.app.annotation.exception;

public class ResourceNotFoundException extends RuntimeException {
public ResourceNotFoundException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.app.annotation.model;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.HashMap;
import java.util.Map;

@NoArgsConstructor
@Getter
@Setter
public class FragmentSelector extends Selector {

private String value;

private String conformsTo;

@Override
public Map<String, Object> toJSON() {
Map<String, Object> json = new HashMap<>();
if (this.type != null) json.put("type", this.type);
if (this.value != null) json.put("value", this.value);
if (this.conformsTo != null) json.put("conformsTo", this.conformsTo);
return json;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,17 @@ public Map<String, Object> toJSON() {
if (this.format != null) json.put("format", this.format);
if (this.textDirection != null) json.put("textDirection", this.textDirection);
if (this.source != null) json.put("source", this.source);
if (this.selector != null) {
List<Map<String, Object>> selectorList = new ArrayList<>();

for (Selector s : this.selector) {
selectorList.add(s.toJSON());
if (this.selector != null && !this.selector.isEmpty()) {
if (this.selector.size() > 1) {
List<Map<String, Object>> selectorList = new ArrayList<>();

for (Selector s : this.selector) {
selectorList.add(s.toJSON());
}
json.put("selector", selectorList);
} else {
json.put("selector", this.selector.get(0).toJSON());
}
json.put("selector", selectorList);
}
return json;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.app.annotation.dto.request.SelectorDto;
import com.app.annotation.dto.request.TargetDto;
import com.app.annotation.exception.BadRequestException;
import com.app.annotation.model.*;
import com.app.annotation.repository.AnnotationRepository;
import com.app.annotation.dto.request.CreateAnnotationRequestDto;
Expand Down Expand Up @@ -36,22 +37,20 @@ protected void configure() {

public Map<String, Object> createAnnotation(CreateAnnotationRequestDto dto) {

Optional<Annotation> prevAnnotation = annotationRepository.findById(dto.getId());

if (prevAnnotation.isPresent()) {
throw new BadRequestException("There is an annotation with same id.");
}

Annotation annotationToCreate = modelMapper.map(dto, Annotation.class);

List<Selector> selectorList = new ArrayList<>();
for (SelectorDto s : dto.getTarget().getSelector()) {
if (s.getExact() != null) {
System.out.println(s.getType());
System.out.println(s.getExact());
}
if (s.getStart() != null) {
System.out.println(s.getType());
System.out.println(s.getStart());
}
if (s.getType().equals("TextQuoteSelector")) {
selectorList.add(modelMapper.map(s, TextQuoteSelector.class));
} else if (s.getType().equals("TextPositionSelector")) {
selectorList.add(modelMapper.map(s, TextPositionSelector.class));
switch (s.getType()) {
case "TextQuoteSelector" -> selectorList.add(modelMapper.map(s, TextQuoteSelector.class));
case "TextPositionSelector" -> selectorList.add(modelMapper.map(s, TextPositionSelector.class));
case "FragmentSelector" -> selectorList.add(modelMapper.map(s, FragmentSelector.class));
}
//TODO add more selectors if implemented in the future
}
Expand Down Expand Up @@ -101,8 +100,39 @@ public List<Map<String, Object>> getAnnotations(String source) {
List<Annotation> annotations = annotationRepository.findAllByTargetSource(source);
List<Map<String, Object>> jsonAnnotations = new ArrayList<>();
for (Annotation a : annotations) {

List<Selector> selectors = a.getTarget().getSelector();

boolean haveTextSelectors = true;

for (Selector s : selectors) {
if (!s.getType().equals("TextQuoteSelector") && !s.getType().equals("TextPositionSelector")) {
haveTextSelectors = false;
break;
}

// TODO change logic as more selectors are implemented in the future
}

if (!haveTextSelectors) {
continue;
}

jsonAnnotations.add(a.toJSON());
}
return jsonAnnotations;
}

public List<Map<String, Object>> getImageAnnotations(String source) {
List<Annotation> annotations = annotationRepository.findAllByTargetSource(source);
List<Map<String, Object>> jsonAnnotations = new ArrayList<>();
for (Annotation a : annotations) {

if (a.getTarget().getSelector().size() == 1 && a.getTarget().getSelector().get(0).getType().equals("FragmentSelector")) {
jsonAnnotations.add(a.toJSON());
}
// TODO change logic if more image selectors are implemented in the future
}
return jsonAnnotations;
}
}
5 changes: 5 additions & 0 deletions app/backend/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
target
images
README.md
format.sh
.gitignore
3 changes: 2 additions & 1 deletion app/backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ build/
*.env

### Image Files
/images/
images/*
!images/README.md
11 changes: 6 additions & 5 deletions app/backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
FROM eclipse-temurin:17-jdk-alpine
WORKDIR /app
VOLUME /app/images
COPY target/*.jar /app/bounswe.jar
FROM maven:3.9.6-eclipse-temurin-21-jammy
WORKDIR /app/backend
VOLUME /app/backend/images
COPY . /app/backend/
RUN mvn clean install
EXPOSE 8080
ENTRYPOINT ["java","-jar","/app/bounswe.jar"]
ENTRYPOINT ["java","-jar","/app/backend/target/gamereview-0.0.1-SNAPSHOT.jar"]
1 change: 1 addition & 0 deletions app/backend/images/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This directory is where the application stores its images, when it is run locally.
19 changes: 19 additions & 0 deletions app/backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,25 @@
<artifactId>spring-javaformat-maven-plugin</artifactId>
<version>0.0.39</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.11</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>generate-code-coverage-report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class CorsConfig {
@Bean
public CorsConfigurationSource corsConfigurationSource() {
final CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(Arrays.asList("http://localhost:5173", "http://ec2-51-20-78-40.eu-north-1.compute.amazonaws.com/"));
configuration.setAllowedOrigins(Arrays.asList("http://localhost:5173", "http://localhost", "http://ec2-51-20-78-40.eu-north-1.compute.amazonaws.com/"));
configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "OPTIONS"));
configuration.setAllowCredentials(true);
configuration.setAllowedHeaders(Arrays.asList("Authorization", "Content-Type", "Set-Cookie", "credentials"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/api/group")
Expand Down Expand Up @@ -184,11 +185,16 @@ public ResponseEntity<List<GroupApplicationResponseDto>> listApplications(@Reque
return ResponseEntity.ok(applications);
}

@AuthorizationRequired
@GetMapping("/get-recommendations")
public ResponseEntity<List<Group>> getRecommendedGroups(@RequestHeader String Authorization, HttpServletRequest request) {
User user = (User) request.getAttribute("authenticatedUser");
List<Group> groups = groupService.getRecommendedGroups(user);
public ResponseEntity<List<Group>> getRecommendedGroups(@RequestHeader(name = HttpHeaders.AUTHORIZATION,
required = false) String Authorization) {
String email = "";
if(Authorization == null){
return ResponseEntity.ok(groupService.getRecommendedGroups(email));
}
if (JwtUtil.validateToken(Authorization))
email = JwtUtil.extractSubject(Authorization);
List<Group> groups = groupService.getRecommendedGroups(email);
return ResponseEntity.ok(groups);
}
}
Loading

0 comments on commit 82f9690

Please sign in to comment.