-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/34 finance service balance crud #97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
...spring-finance/src/main/java/tum/devoops/financeservice/controller/FinanceController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| package tum.devoops.financeservice.controller; | ||
|
|
||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.security.access.prepost.PreAuthorize; | ||
| import org.springframework.security.core.Authentication; | ||
| import org.springframework.security.core.context.SecurityContextHolder; | ||
| import org.springframework.security.oauth2.jwt.Jwt; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
| import tum.devoops.financeservice.api.FinanceApi; | ||
| import tum.devoops.financeservice.model.Balance; | ||
| import tum.devoops.financeservice.model.Transaction; | ||
| import tum.devoops.financeservice.model.TransactionCreate; | ||
| import tum.devoops.financeservice.model.TransactionPartialUpdate; | ||
| import tum.devoops.financeservice.service.TransactionService; | ||
|
|
||
| import java.util.List; | ||
| import java.util.UUID; | ||
|
|
||
| @RestController | ||
| @PreAuthorize("hasAnyRole('admin', 'member')") | ||
| public class FinanceController implements FinanceApi { | ||
|
|
||
| @Autowired | ||
| TransactionService transactionService; | ||
|
|
||
| @Override | ||
| public ResponseEntity<Transaction> createTransaction(TransactionCreate transactionCreate) { | ||
| Authentication auth = SecurityContextHolder.getContext().getAuthentication(); | ||
| UUID requesterId = extractRequesterId(auth); | ||
| boolean isAdmin = extractIsAdmin(auth); | ||
| Transaction created = transactionService.createTransaction(transactionCreate, requesterId, isAdmin); | ||
| return ResponseEntity.status(HttpStatus.CREATED).body(created); | ||
| } | ||
|
|
||
| @Override | ||
| public ResponseEntity<Void> deleteTransaction(UUID transactionId) { | ||
| Authentication auth = SecurityContextHolder.getContext().getAuthentication(); | ||
| UUID requesterId = extractRequesterId(auth); | ||
| boolean isAdmin = extractIsAdmin(auth); | ||
| transactionService.deleteTransaction(transactionId, requesterId, isAdmin); | ||
| return ResponseEntity.noContent().build(); | ||
| } | ||
|
|
||
| @Override | ||
| public ResponseEntity<List<Transaction>> getAllTransactions() { | ||
| Authentication auth = SecurityContextHolder.getContext().getAuthentication(); | ||
| UUID requesterId = extractRequesterId(auth); | ||
| boolean isAdmin = extractIsAdmin(auth); | ||
| return ResponseEntity.ok(transactionService.getAllTransactions(requesterId, isAdmin)); | ||
| } | ||
|
|
||
| @Override | ||
| public ResponseEntity<Transaction> getTransaction(UUID transactionId) { | ||
| Authentication auth = SecurityContextHolder.getContext().getAuthentication(); | ||
| UUID requesterId = extractRequesterId(auth); | ||
| boolean isAdmin = extractIsAdmin(auth); | ||
| return ResponseEntity.ok(transactionService.getTransaction(transactionId, requesterId, isAdmin)); | ||
| } | ||
|
|
||
| @Override | ||
| public ResponseEntity<Transaction> updateTransaction(UUID transactionId, TransactionPartialUpdate transactionPartialUpdate) { | ||
| Authentication auth = SecurityContextHolder.getContext().getAuthentication(); | ||
| UUID requesterId = extractRequesterId(auth); | ||
| boolean isAdmin = extractIsAdmin(auth); | ||
| return ResponseEntity.ok(transactionService.updateTransaction(transactionId, transactionPartialUpdate, requesterId, isAdmin)); | ||
| } | ||
|
|
||
| @Override | ||
| public ResponseEntity<List<Balance>> getAllBalances() { | ||
| Authentication auth = SecurityContextHolder.getContext().getAuthentication(); | ||
| UUID requesterId = extractRequesterId(auth); | ||
| boolean isAdmin = extractIsAdmin(auth); | ||
| return ResponseEntity.ok(transactionService.getAllBalances(requesterId, isAdmin)); | ||
| } | ||
|
|
||
| @Override | ||
| public ResponseEntity<Balance> getMemberBalance(UUID memberId) { | ||
| Authentication auth = SecurityContextHolder.getContext().getAuthentication(); | ||
| UUID requesterId = extractRequesterId(auth); | ||
| boolean isAdmin = extractIsAdmin(auth); | ||
| return ResponseEntity.ok(transactionService.getMemberBalance(memberId, requesterId, isAdmin)); | ||
| } | ||
|
|
||
| private UUID extractRequesterId(Authentication auth) { | ||
| Jwt jwt = (Jwt) auth.getPrincipal(); | ||
| return UUID.fromString(jwt.getSubject()); | ||
| } | ||
|
|
||
| private boolean extractIsAdmin(Authentication auth) { | ||
| return auth.getAuthorities().stream() | ||
| .anyMatch(a -> "ROLE_admin".equals(a.getAuthority())); | ||
| } | ||
| } |
2 changes: 1 addition & 1 deletion
2
...voops/financeservice/HelloController.java → ...ceservice/controller/HelloController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...ring-finance/src/main/java/tum/devoops/financeservice/converter/TransactionConverter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package tum.devoops.financeservice.converter; | ||
|
|
||
| import tum.devoops.financeservice.entity.TransactionEntity; | ||
| import tum.devoops.financeservice.model.Transaction; | ||
| import tum.devoops.financeservice.model.TransactionCreate; | ||
|
|
||
| import java.time.Instant; | ||
| import java.time.ZoneOffset; | ||
| import java.util.UUID; | ||
|
|
||
| public class TransactionConverter { | ||
|
|
||
| public static Transaction toTransaction(TransactionEntity entity) { | ||
| return new Transaction( | ||
| entity.getId(), | ||
| entity.getMemberId().toString(), | ||
| entity.getCreatorId().toString(), | ||
| entity.getAmountCents(), | ||
| entity.getCreatedAt().atOffset(ZoneOffset.UTC), | ||
| entity.getTitle(), | ||
| entity.getDescription() | ||
| ); | ||
| } | ||
|
|
||
| public static TransactionEntity toEntity(TransactionCreate create, UUID memberId, UUID creatorId) { | ||
| TransactionEntity entity = new TransactionEntity(); | ||
| entity.setMemberId(memberId); | ||
| entity.setCreatorId(creatorId); | ||
| entity.setAmountCents(create.getAmountCents()); | ||
| entity.setCreatedAt(Instant.now()); | ||
| entity.setTitle(create.getTitle()); | ||
| entity.setDescription(create.getDescription() != null ? create.getDescription() : ""); | ||
| return entity; | ||
| } | ||
| } |
32 changes: 32 additions & 0 deletions
32
services/spring-finance/src/main/java/tum/devoops/financeservice/entity/DirectorEntity.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package tum.devoops.financeservice.entity; | ||
|
|
||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.Table; | ||
| import jakarta.persistence.Embeddable; | ||
| import jakarta.persistence.EmbeddedId; | ||
| import lombok.Getter; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Data; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| import java.io.Serializable; | ||
| import java.util.UUID; | ||
|
|
||
| @Entity | ||
| @Table(schema = "organization", name = "directors") | ||
| @Getter @NoArgsConstructor | ||
| public class DirectorEntity { | ||
| @EmbeddedId | ||
| private Id id; | ||
|
|
||
| @Embeddable | ||
| @Data @NoArgsConstructor @AllArgsConstructor | ||
| public static class Id implements Serializable { | ||
| @Column(name = "sport_name", nullable = false) | ||
| private String sportName; | ||
|
|
||
| @Column(name = "member_id", nullable = false) | ||
| private UUID memberId; | ||
| } | ||
| } |
20 changes: 20 additions & 0 deletions
20
services/spring-finance/src/main/java/tum/devoops/financeservice/entity/MemberEntity.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package tum.devoops.financeservice.entity; | ||
|
|
||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.Table; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| @Entity | ||
| @Table(schema = "member", name="members") | ||
| @Getter | ||
| @NoArgsConstructor | ||
| public class MemberEntity { | ||
| @Id | ||
| @Column(name = "id", nullable = false, updatable = false) | ||
| private UUID id; | ||
| } |
35 changes: 35 additions & 0 deletions
35
services/spring-finance/src/main/java/tum/devoops/financeservice/entity/TeamEntity.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package tum.devoops.financeservice.entity; | ||
|
|
||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.Table; | ||
| import jakarta.persistence.OneToMany; | ||
| import jakarta.persistence.JoinColumn; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| import java.util.List; | ||
| import java.util.UUID; | ||
|
|
||
| @Entity | ||
| @Table(schema = "organization", name = "teams") | ||
| @Getter | ||
| @NoArgsConstructor | ||
| public class TeamEntity { | ||
|
|
||
| @Id | ||
| @Column(name = "id", nullable = false) | ||
| UUID id; | ||
|
|
||
| @Column(name = "sport_name", nullable = false) | ||
| private String sportName; | ||
|
|
||
| @OneToMany | ||
| @JoinColumn(name = "team_id", referencedColumnName = "id", insertable = false, updatable = false) | ||
| private List<TrainerEntity> trainers; | ||
|
|
||
| @OneToMany | ||
| @JoinColumn(name = "team_id", referencedColumnName = "id", insertable = false, updatable = false) | ||
| private List<TraineeEntity> trainees; | ||
| } | ||
32 changes: 32 additions & 0 deletions
32
services/spring-finance/src/main/java/tum/devoops/financeservice/entity/TraineeEntity.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package tum.devoops.financeservice.entity; | ||
|
|
||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.Table; | ||
| import jakarta.persistence.EmbeddedId; | ||
| import jakarta.persistence.Embeddable; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Data; | ||
| import java.io.Serializable; | ||
| import java.util.UUID; | ||
|
|
||
| @Entity | ||
| @Table(schema = "organization", name = "trainees") | ||
| @Getter | ||
| @NoArgsConstructor @AllArgsConstructor | ||
| public class TraineeEntity { | ||
| @EmbeddedId | ||
| private Id id; | ||
|
|
||
| @Embeddable | ||
| @Data @NoArgsConstructor @AllArgsConstructor | ||
| public static class Id implements Serializable { | ||
| @Column(name = "team_id", nullable = false) | ||
| private UUID teamId; | ||
|
|
||
| @Column(name = "member_id", nullable = false) | ||
| private UUID memberId; | ||
| } | ||
| } |
31 changes: 31 additions & 0 deletions
31
services/spring-finance/src/main/java/tum/devoops/financeservice/entity/TrainerEntity.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package tum.devoops.financeservice.entity; | ||
|
|
||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.Table; | ||
| import jakarta.persistence.EmbeddedId; | ||
| import jakarta.persistence.Embeddable; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Data; | ||
| import java.io.Serializable; | ||
| import java.util.UUID; | ||
|
|
||
| @Entity | ||
| @Table(schema = "organization", name = "trainers") | ||
| @Getter | ||
| public class TrainerEntity { | ||
| @EmbeddedId | ||
| private Id id; | ||
|
|
||
| @Embeddable | ||
| @Data @NoArgsConstructor @AllArgsConstructor | ||
| public static class Id implements Serializable { | ||
| @Column(name = "team_id", nullable = false) | ||
| private UUID teamId; | ||
|
|
||
| @Column(name = "member_id", nullable = false) | ||
| private UUID memberId; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
...pring-finance/src/main/java/tum/devoops/financeservice/exception/BadRequestException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package tum.devoops.financeservice.exception; | ||
|
|
||
| public class BadRequestException extends RuntimeException { | ||
| public BadRequestException(String message) { | ||
| super(message); | ||
| } | ||
| } |
7 changes: 7 additions & 0 deletions
7
...spring-finance/src/main/java/tum/devoops/financeservice/exception/ForbiddenException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package tum.devoops.financeservice.exception; | ||
|
|
||
| public class ForbiddenException extends RuntimeException { | ||
| public ForbiddenException(String message) { | ||
| super(message); | ||
| } | ||
| } |
31 changes: 31 additions & 0 deletions
31
...ng-finance/src/main/java/tum/devoops/financeservice/exception/GlobalExceptionHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package tum.devoops.financeservice.exception; | ||
|
|
||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.ExceptionHandler; | ||
| import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
| import tum.devoops.financeservice.model.BadRequestResponse; | ||
| import tum.devoops.financeservice.model.ErrorResponse; | ||
|
|
||
| @RestControllerAdvice | ||
| public class GlobalExceptionHandler { | ||
|
|
||
| @ExceptionHandler(NotFoundException.class) | ||
| public ResponseEntity<ErrorResponse> handleNotFound(NotFoundException ex) { | ||
| return ResponseEntity.status(HttpStatus.NOT_FOUND) | ||
| .body(new ErrorResponse().message(ex.getMessage())); | ||
| } | ||
|
|
||
| @ExceptionHandler(ForbiddenException.class) | ||
| public ResponseEntity<ErrorResponse> handleForbidden(ForbiddenException ex) { | ||
| return ResponseEntity.status(HttpStatus.FORBIDDEN) | ||
| .body(new ErrorResponse().message(ex.getMessage())); | ||
| } | ||
|
|
||
| @ExceptionHandler(BadRequestException.class) | ||
| public ResponseEntity<BadRequestResponse> handleBadRequest(BadRequestException ex) { | ||
| return ResponseEntity.status(HttpStatus.BAD_REQUEST) | ||
| .body(new BadRequestResponse().message(ex.getMessage())); | ||
| } | ||
|
|
||
| } |
7 changes: 7 additions & 0 deletions
7
.../spring-finance/src/main/java/tum/devoops/financeservice/exception/NotFoundException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package tum.devoops.financeservice.exception; | ||
|
|
||
| public class NotFoundException extends RuntimeException { | ||
| public NotFoundException(String message) { | ||
| super(message); | ||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
14
...pring-finance/src/main/java/tum/devoops/financeservice/repository/DirectorRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package tum.devoops.financeservice.repository; | ||
|
|
||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.data.jpa.repository.Query; | ||
| import org.springframework.data.repository.query.Param; | ||
| import tum.devoops.financeservice.entity.DirectorEntity; | ||
|
|
||
| import java.util.List; | ||
| import java.util.UUID; | ||
|
|
||
| public interface DirectorRepository extends JpaRepository<DirectorEntity, DirectorEntity.Id> { | ||
| @Query("SELECT d.id.sportName FROM DirectorEntity d WHERE d.id.memberId = :memberId") | ||
| List<String> findSportNamesByMemberId(@Param("memberId") UUID memberId); | ||
| } |
9 changes: 9 additions & 0 deletions
9
.../spring-finance/src/main/java/tum/devoops/financeservice/repository/MemberRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package tum.devoops.financeservice.repository; | ||
|
|
||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import tum.devoops.financeservice.entity.MemberEntity; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| public interface MemberRepository extends JpaRepository<MemberEntity, UUID> { | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🌐 Web query:
Does JPQL allow selecting a collection-valued path expression likeSELECT t.trainees FROM TeamEntity t, or must it useJOIN t.trainees traineeand select the joined alias?💡 Result:
JPQL does not allow selecting a collection-valued path expression directly in the SELECT clause [1][2][3]. A query such as SELECT t.trainees FROM TeamEntity t is invalid because the SELECT clause must contain only single-valued expressions [2][3][4]. To achieve this, you must use a JOIN in the FROM clause to define an identification variable for the elements within the collection, and then select that alias [1][2][4]. The correct approach is: SELECT trainee FROM TeamEntity t JOIN t.trainees trainee Alternatively, using the IN syntax: SELECT trainee FROM TeamEntity t, IN (t.trainees) trainee In both cases, you are selecting the single-valued alias (trainee) that represents an individual element of the collection, which is permitted by the JPQL specification [2][4][5].
Citations:
🏁 Script executed:
Repository: AET-DevOps26/team-devoops
Length of output: 2036
🏁 Script executed:
Repository: AET-DevOps26/team-devoops
Length of output: 237
🏁 Script executed:
Repository: AET-DevOps26/team-devoops
Length of output: 4622
Use a JOIN in the trainee repository queries
SELECT t.traineesis not valid JPQL for a collection-valued association. BothfindTraineesBySportNameandfindTraineesByTeamIdshould joint.traineesand select the joined alias, otherwise the authorization flow inTransactionServicecan fail when these queries are parsed or executed.🤖 Prompt for AI Agents