Skip to content

Commit

Permalink
Rename chat to session
Browse files Browse the repository at this point in the history
  • Loading branch information
milesha committed Nov 19, 2024
1 parent 945bc30 commit 4a5c287
Show file tree
Hide file tree
Showing 19 changed files with 325 additions and 226 deletions.
99 changes: 99 additions & 0 deletions server/application-server/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,42 @@ servers:
- url: /
description: Default Server URL
paths:
/session:
post:
tags:
- session
operationId: createSession
requestBody:
content:
application/json:
schema:
$ref: "#/components/schemas/User"
required: true
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: "#/components/schemas/Session"
/message:
post:
tags:
- message
operationId: sendMessage
requestBody:
content:
application/json:
schema:
$ref: "#/components/schemas/MessageDTO"
required: true
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: "#/components/schemas/Message"
/user/{login}/profile:
get:
tags:
Expand All @@ -31,6 +67,25 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/UserProfile"
/session/session/{sessionId}:
get:
tags:
- session
operationId: getSession
parameters:
- name: sessionId
in: path
required: true
schema:
type: integer
format: int64
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: "#/components/schemas/Session"
/meta:
get:
tags:
Expand Down Expand Up @@ -181,6 +236,30 @@ components:
type: string
color:
type: string
Message:
required:
- content
- id
- sender
- sentAt
- session
type: object
properties:
id:
type: integer
format: int64
sentAt:
type: string
format: date-time
sender:
type: string
enum:
- SYSTEM
- USER
content:
type: string
session:
$ref: "#/components/schemas/Session"
UserProfile:
required:
- contributedRepositories
Expand Down Expand Up @@ -379,3 +458,23 @@ components:
numberOfCodeComments:
type: integer
format: int32
Session:
required:
- creationDate
- id
- messages
- user
type: object
properties:
id:
type: integer
format: int64
messages:
type: array
items:
$ref: "#/components/schemas/Message"
user:
$ref: "#/components/schemas/UserInfo"
creationDate:
type: string
format: date-time
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import de.tum.in.www1.hephaestus.gitprovider.pullrequestreview.PullRequestReviewInfoDTO;
import de.tum.in.www1.hephaestus.gitprovider.repository.RepositoryInfoDTO;
import de.tum.in.www1.hephaestus.gitprovider.user.UserInfoDTO;
import de.tum.in.www1.hephaestus.chat.ChatDTO;
import de.tum.in.www1.hephaestus.chat.SessionDTO;
import de.tum.in.www1.hephaestus.chat.message.MessageDTO;
import io.hypersistence.utils.hibernate.type.util.ClassImportIntegrator;

Expand All @@ -34,7 +34,7 @@ public List<Integrator> getIntegrators() {
classes.add(PullRequestReviewInfoDTO.class);
classes.add(RepositoryInfoDTO.class);
classes.add(MessageDTO.class);
classes.add(ChatDTO.class);
classes.add(SessionDTO.class);

return List.of(new ClassImportIntegrator(classes));
}
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@


@Entity
@Table(name = "chat")
@Table(name = "session")
@Getter
@Setter
@ToString(callSuper = true)
@NoArgsConstructor
public class Chat extends BaseGitServiceEntity {
public class Session extends BaseGitServiceEntity {

@OrderColumn(name = "message_order")
@OneToMany(mappedBy = "chat")
@OneToMany(mappedBy = "session")
private List<Message> messages = new ArrayList<>();

@Column(name = "creation_date")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package de.tum.in.www1.hephaestus.chat;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Optional;

import de.tum.in.www1.hephaestus.gitprovider.user.User;
import org.springframework.http.ResponseEntity;

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

private final SessionService sessionService;

public SessionController(SessionService sessionService) {
this.sessionService = sessionService;
}

@PostMapping
public ResponseEntity<SessionDTO> createSession(@RequestBody User user) {
SessionDTO session = sessionService.createSession(user);
return ResponseEntity.ok(session);
}

@GetMapping("/session/{sessionId}")
public ResponseEntity<SessionDTO> getSession(@PathVariable Long sessionId) {
Optional<SessionDTO> session = sessionService.findSessionById(sessionId);
return ResponseEntity.ok(session.get());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@


@JsonInclude(JsonInclude.Include.NON_EMPTY)
public record ChatDTO (
public record SessionDTO (
@NonNull Long id,
@NonNull List<Message> messages,
@NonNull UserInfoDTO user,
@NonNull ZonedDateTime creationDate){

public ChatDTO(Chat chat) {
this(chat.getId(), chat.getMessages(), UserInfoDTO.fromUser(chat.getUser()), chat.getCreationDate());
public SessionDTO(Session session) {
this(session.getId(), session.getMessages(), UserInfoDTO.fromUser(session.getUser()), session.getCreationDate());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package de.tum.in.www1.hephaestus.chat;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Optional;
import jakarta.persistence.EntityNotFoundException;
import jakarta.validation.constraints.NotNull;
import org.springframework.stereotype.Repository;

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

@Query("""
SELECT s
FROM Session s
LEFT JOIN FETCH s.messages m
WHERE s.id = :sessionId
""")
Optional<Session> findByIdWithMessages(@Param("sessionId") long sessionId);

@Query("""
SELECT s
FROM Session s
LEFT JOIN FETCH s.messages m
LEFT JOIN FETCH m.content c
WHERE s.id = :sessionId
""")
Session findByIdWithMessagesAndContents(@Param("sessionId") long sessionId);

private Session getValueElseThrow(Optional<Session> optional, long sessionId) {
return optional.orElseThrow(() ->
new EntityNotFoundException("Session entity with id " + sessionId + " was not found.")
);
}

@NotNull
default Session findByIdWithMessagesElseThrow(long sessionId) throws EntityNotFoundException {
return getValueElseThrow(findByIdWithMessages(sessionId), sessionId);
}
}
Loading

0 comments on commit 4a5c287

Please sign in to comment.