Skip to content

Commit

Permalink
restructure client code
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixTJDietrich committed Dec 5, 2024
1 parent 80a24bd commit fb80e0b
Show file tree
Hide file tree
Showing 34 changed files with 520 additions and 431 deletions.
30 changes: 5 additions & 25 deletions server/application-server/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -210,17 +210,11 @@ paths:
responses:
"200":
description: OK
/mentor/session:
/mentor/sessions:
get:
tags:
- session
operationId: getSessions
parameters:
- name: login
in: query
required: true
schema:
type: string
operationId: getAllSessions
responses:
"200":
description: OK
Expand All @@ -233,21 +227,15 @@ paths:
post:
tags:
- session
operationId: createSession
requestBody:
content:
application/json:
schema:
type: string
required: true
operationId: createNewSession
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: "#/components/schemas/Session"
/mentor/message/{sessionId}:
/mentor/sessions/{sessionId}:
get:
tags:
- message
Expand Down Expand Up @@ -534,7 +522,7 @@ components:
sender:
type: string
enum:
- LLM
- MENTOR
- USER
content:
type: string
Expand Down Expand Up @@ -801,19 +789,11 @@ components:
required:
- createdAt
- id
- messages
- userLogin
type: object
properties:
id:
type: integer
format: int64
messages:
type: array
items:
$ref: "#/components/schemas/Message"
userLogin:
type: string
createdAt:
type: string
format: date-time
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package de.tum.in.www1.hephaestus;

import java.util.Optional;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.jwt.Jwt;

/**
* Utility class for Spring Security.
*/
public final class SecurityUtils {

private SecurityUtils() {}

/**
* Get the login of the current user.
*
* @return the login of the current user.
*/
public static Optional<String> getCurrentUserLogin() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null) {
return Optional.empty();
}
if (authentication.getPrincipal() instanceof Jwt) {
Jwt jwt = (Jwt) authentication.getPrincipal();
return Optional.ofNullable(jwt.getClaimAsString("preferred_username"));
}
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package de.tum.in.www1.hephaestus.core.exception;

import java.io.Serial;

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

/**
* Generic unchecked exception for access forbidden (i.e. 403) errors.
*/
@ResponseStatus(HttpStatus.FORBIDDEN)
public class AccessForbiddenException extends RuntimeException {

public static final String NOT_ALLOWED = "You are not allowed to access this resource";

@Serial
private static final long serialVersionUID = 1L;

public AccessForbiddenException() {
super(NOT_ALLOWED);
}

public AccessForbiddenException(String message) {
super(message);
}

public AccessForbiddenException(Throwable cause) {
super(NOT_ALLOWED, cause);
}

public AccessForbiddenException(String entityType, long entityId) {
super("You are not allowed to access the " + entityType + " with id " + entityId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package de.tum.in.www1.hephaestus.core.exception;

import java.io.Serial;

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

@ResponseStatus(HttpStatus.NOT_FOUND)
public class EntityNotFoundException extends RuntimeException {

@Serial
private static final long serialVersionUID = 1L;

public EntityNotFoundException() {
super();
}

public EntityNotFoundException(String message) {
super(message);
}

public EntityNotFoundException(String entityName, Long entityId) {
super(entityName + " with id: \"" + entityId + "\" does not exist");
}

public EntityNotFoundException(String entityName, String entityIdentifier) {
super(entityName + " with identifier: \"" + entityIdentifier + "\" does not exist");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.tum.in.www1.hephaestus.gitprovider.user;

import de.tum.in.www1.hephaestus.SecurityUtils;
import java.util.List;
import java.util.Optional;
import java.util.Set;
Expand All @@ -21,7 +22,7 @@ public interface UserRepository extends JpaRepository<User, Long> {
SELECT u
FROM User u
LEFT JOIN FETCH u.mergedPullRequests
WHERE u.login = :login
WHERE u.login ILIKE :login
""")
Optional<User> findByLoginWithEagerMergedPullRequests(@Param("login") String login);

Expand Down Expand Up @@ -69,4 +70,12 @@ NOT EXISTS (SELECT l
)
""")
Set<User> findAllContributingToTeam(@Param("teamId") Long teamId);

/**
* @return existing user object by current user login
*/
default Optional<User> getCurrentUser() {
var currentUserLogin = SecurityUtils.getCurrentUserLogin();
return currentUserLogin.map(this::findByLogin).orElse(Optional.empty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ public class Message {
private Session session;

public enum MessageSender {
LLM, USER
MENTOR, USER
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package de.tum.in.www1.hephaestus.mentor.message;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -8,23 +10,56 @@
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import de.tum.in.www1.hephaestus.gitprovider.user.UserRepository;
import de.tum.in.www1.hephaestus.mentor.session.SessionRepository;
import de.tum.in.www1.hephaestus.mentor.session.SessionService;

@RestController
@RequestMapping("/mentor/message")
@RequestMapping("/mentor/sessions")
public class MessageController {

@Autowired
private UserRepository userRepository;

@Autowired
private SessionRepository sessionRepository;

@Autowired
private SessionService sessionService;

@Autowired
private MessageService messageService;

@GetMapping("/{sessionId}")
public ResponseEntity<List<MessageDTO>> getMessages(@PathVariable Long sessionId) {
var user = userRepository.getCurrentUser();
if (user.isEmpty()) {
return ResponseEntity.notFound().build();
}

var session = sessionRepository.findById(sessionId);
if (session.isEmpty()) {
return ResponseEntity.notFound().build();
}
sessionService.checkAccessElseThrow(user.get(), session.get());

List<MessageDTO> messages = messageService.getMessagesBySessionId(sessionId);
return ResponseEntity.ok(messages);
}

@PostMapping("/{sessionId}")
public ResponseEntity<MessageDTO> createMessage(@RequestBody String message, @PathVariable Long sessionId) {
var user = userRepository.getCurrentUser();
if (user.isEmpty()) {
return ResponseEntity.notFound().build();
}

var session = sessionRepository.findById(sessionId);
if (session.isEmpty()) {
return ResponseEntity.notFound().build();
}
sessionService.checkAccessElseThrow(user.get(), session.get());

MessageDTO createdMessage = messageService.sendMessage(message, sessionId);
return ResponseEntity.ok(createdMessage);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public MessageDTO sendMessage(String content, Long sessionId) {
}

Message systemMessage = new Message();
systemMessage.setSender(MessageSender.LLM);
systemMessage.setSender(MessageSender.MENTOR);
systemMessage.setContent(systemResponse);
systemMessage.setSession(currentSession);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,45 @@
package de.tum.in.www1.hephaestus.mentor.session;

import java.util.List;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;

import de.tum.in.www1.hephaestus.gitprovider.user.UserRepository;

@RestController
@RequestMapping("/mentor/session")
@RequestMapping("/mentor/sessions")
public class SessionController {

@Autowired
private UserRepository userRepository;

@Autowired
private SessionService sessionService;

@GetMapping
public ResponseEntity<List<SessionDTO>> getSessions(@RequestParam String login) {
List<SessionDTO> sessions = sessionService.findAllSessionsByUser(login);
public ResponseEntity<List<SessionDTO>> getAllSessions() {
var user = userRepository.getCurrentUser();
if (user.isEmpty()) {
return ResponseEntity.notFound().build();
}

List<SessionDTO> sessions = sessionService.findAllSessionsByUser(user.get());
return ResponseEntity.ok(sessions);
}

@PostMapping
public ResponseEntity<SessionDTO> createSession(@RequestBody String login) {
SessionDTO session = sessionService.createSession(login);
public ResponseEntity<SessionDTO> createNewSession() {
var user = userRepository.getCurrentUser();
if (user.isEmpty()) {
return ResponseEntity.notFound().build();
}

SessionDTO session = sessionService.createSession(user.get());
return ResponseEntity.ok(session);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,16 @@

import org.springframework.lang.NonNull;
import com.fasterxml.jackson.annotation.JsonInclude;
import de.tum.in.www1.hephaestus.mentor.message.MessageDTO;
import java.util.List;
import java.time.OffsetDateTime;

@JsonInclude(JsonInclude.Include.NON_EMPTY)
public record SessionDTO(
@NonNull Long id,
@NonNull List<MessageDTO> messages,
@NonNull String userLogin,
@NonNull OffsetDateTime createdAt) {

public static SessionDTO fromSession(Session session) {
return new SessionDTO(
session.getId(),
session.getMessages().stream().map(MessageDTO::fromMessage).toList(),
session.getUser().getLogin(),
session.getCreatedAt());
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package de.tum.in.www1.hephaestus.mentor.session;

import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;
import de.tum.in.www1.hephaestus.gitprovider.user.User;

@Repository
public interface SessionRepository extends JpaRepository<Session, Long> {

List<Session> findByUserLogin(String login);
List<Session> findByUser(User user);
}
Loading

0 comments on commit fb80e0b

Please sign in to comment.