Skip to content

Commit

Permalink
Merge pull request #16 from volo-db/15-add-user-endpoint
Browse files Browse the repository at this point in the history
feat: add new user endpoint
  • Loading branch information
fabian-urner authored May 2, 2024
2 parents 8daa607 + 37f85ae commit 4ff64ad
Show file tree
Hide file tree
Showing 12 changed files with 209 additions and 213 deletions.
157 changes: 79 additions & 78 deletions sql/insert_data.sql

Large diffs are not rendered by default.

13 changes: 7 additions & 6 deletions sql/volodb.sql
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@ CREATE TABLE `contact_type` (

DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`username` VARCHAR(50) NOT NULL,
`email` VARCHAR(50) NOT NULL,
`person` INT NOT NULL,
`secret` VARCHAR(100) NOT NULL,
`organisational_role` VARCHAR(50),
PRIMARY KEY (`username`)
`avatar` TEXT,
PRIMARY KEY (`email`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;

DROP TABLE IF EXISTS `user_role`;
Expand Down Expand Up @@ -319,7 +320,7 @@ CREATE TABLE `volunteer_document_type` (

ALTER TABLE `user` ADD FOREIGN KEY (`person`) REFERENCES `person` (`id`);

ALTER TABLE `user_role_mapping` ADD FOREIGN KEY (`user`) REFERENCES `user` (`username`);
ALTER TABLE `user_role_mapping` ADD FOREIGN KEY (`user`) REFERENCES `user` (`email`);

ALTER TABLE `user_role_mapping` ADD FOREIGN KEY (`user_role`) REFERENCES `user_role` (`id`);

Expand Down Expand Up @@ -351,7 +352,7 @@ ALTER TABLE `volunteer` ADD FOREIGN KEY (`religion`) REFERENCES `religion` (`id`

ALTER TABLE `editing_history` ADD FOREIGN KEY (`volunteer`) REFERENCES `volunteer` (`id`);

ALTER TABLE `editing_history` ADD FOREIGN KEY (`user`) REFERENCES `user` (`username`);
ALTER TABLE `editing_history` ADD FOREIGN KEY (`user`) REFERENCES `user` (`email`);

ALTER TABLE `editing_history` ADD FOREIGN KEY (`editing_type`) REFERENCES `editing_type` (`id`);

Expand Down Expand Up @@ -383,10 +384,10 @@ ALTER TABLE `contract_modification` ADD FOREIGN KEY (`status`) REFERENCES `contr

ALTER TABLE `volunteer_note` ADD FOREIGN KEY (`volunteer`) REFERENCES `volunteer` (`id`);

ALTER TABLE `volunteer_note` ADD FOREIGN KEY (`user`) REFERENCES `user` (`username`);
ALTER TABLE `volunteer_note` ADD FOREIGN KEY (`user`) REFERENCES `user` (`email`);

ALTER TABLE `volunteer_document` ADD FOREIGN KEY (`volunteer`) REFERENCES `volunteer` (`id`);

ALTER TABLE `volunteer_document` ADD FOREIGN KEY (`type`) REFERENCES `volunteer_document_type` (`id`);

ALTER TABLE `volunteer_document` ADD FOREIGN KEY (`user`) REFERENCES `user` (`username`);
ALTER TABLE `volunteer_document` ADD FOREIGN KEY (`user`) REFERENCES `user` (`email`);
30 changes: 14 additions & 16 deletions src/main/java/dev/urner/volodb/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,36 @@
import lombok.Getter;
import lombok.Setter;


@Entity
@Table(name="User")
@Table(name = "User")
@Getter
@Setter
public class User {
//Username

// Username
@Id
@Column(name = "username")
@Column(name = "email")
private String username;

@OneToOne
@JoinColumn(name = "person") //FK
@JoinColumn(name = "person") // FK
private Person person;

//Secret
// Secret
@JsonIgnore
@Column(name = "secret")
private String secret;
//user_roles

// user_roles
@ManyToMany
@JoinTable(
name = "user_role_mapping",
joinColumns = @JoinColumn(name = "user"),
inverseJoinColumns = @JoinColumn(name = "user_role"))
private List<UserRole> roles;
@JoinTable(name = "user_role_mapping", joinColumns = @JoinColumn(name = "user"), inverseJoinColumns = @JoinColumn(name = "user_role"))
private List<UserRole> roles;

//organisational_role
// organisational_role
@Column(name = "organisationalRole")
private String organisationalRole;


@Column(name = "avatar")
private String avatar;

}
18 changes: 10 additions & 8 deletions src/main/java/dev/urner/volodb/rest/AuthRestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,25 @@ public LoginResponse login(@RequestBody @Validated LoginRequest request) {
}

@ExceptionHandler
public ResponseEntity<AuthErrorResponse> handleException(UserNotFoundException exc) {
AuthErrorResponse error = new AuthErrorResponse(
HttpStatus.NOT_FOUND.value(),
public ResponseEntity<VolodbErrorResponse> handleException(UserNotFoundException exc) {
HttpStatus httpStatus = HttpStatus.NOT_FOUND;
VolodbErrorResponse error = new VolodbErrorResponse(
httpStatus.value(),
exc.getMessage(),
System.currentTimeMillis());

return new ResponseEntity<>(error, HttpStatus.NOT_FOUND);
return new ResponseEntity<>(error, httpStatus);
}

@ExceptionHandler
public ResponseEntity<AuthErrorResponse> handleException(TokenExpiredException exc) {
AuthErrorResponse error = new AuthErrorResponse(
HttpStatus.UNAUTHORIZED.value(),
public ResponseEntity<VolodbErrorResponse> handleException(TokenExpiredException exc) {
HttpStatus httpStatus = HttpStatus.UNAUTHORIZED;
VolodbErrorResponse error = new VolodbErrorResponse(
httpStatus.value(),
exc.getMessage(),
System.currentTimeMillis());

return new ResponseEntity<>(error, HttpStatus.UNAUTHORIZED);
return new ResponseEntity<>(error, httpStatus);
}

}
16 changes: 0 additions & 16 deletions src/main/java/dev/urner/volodb/rest/ContractErrorResponse.java

This file was deleted.

14 changes: 7 additions & 7 deletions src/main/java/dev/urner/volodb/rest/FileRestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ public void getTestMethod(@PathVariable("bucket") String bucket, HttpServletRequ
}

@ExceptionHandler
public ResponseEntity<VolunteerErrorResponse> handleException(RuntimeException exc) {
VolunteerErrorResponse error = new VolunteerErrorResponse();
public ResponseEntity<VolodbErrorResponse> handleException(RuntimeException exc) {

HttpStatus status = HttpStatus.BAD_REQUEST;
HttpStatus httpStatus = HttpStatus.BAD_REQUEST;

error.setStatus(status.value());
error.setMessage(exc.getMessage());
error.setTimeStamp(System.currentTimeMillis());
VolodbErrorResponse error = new VolodbErrorResponse(
httpStatus.value(),
exc.getMessage(),
System.currentTimeMillis());

return new ResponseEntity<>(error, status);
return new ResponseEntity<>(error, httpStatus);
}

}
16 changes: 0 additions & 16 deletions src/main/java/dev/urner/volodb/rest/ProjectErrorResponse.java

This file was deleted.

46 changes: 21 additions & 25 deletions src/main/java/dev/urner/volodb/rest/ProjectRestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import dev.urner.volodb.service.ProjectService;
import lombok.RequiredArgsConstructor;

import java.util.List;
import java.lang.System;
import java.util.Map;

@RestController
Expand Down Expand Up @@ -89,53 +89,49 @@ public String deleteProjectById(@PathVariable int projectId) {
// Exception-Hanlder:

@ExceptionHandler
public ResponseEntity<ProjectErrorResponse> handleException(ProjectNotFoundException exc) {
ProjectErrorResponse error = new ProjectErrorResponse();

public ResponseEntity<VolodbErrorResponse> handleException(ProjectNotFoundException exc) {
HttpStatus httpStatus = HttpStatus.NOT_FOUND;

error.setStatus(httpStatus.value());
error.setMessage(exc.getMessage());
error.setTimeStamp(System.currentTimeMillis());
VolodbErrorResponse error = new VolodbErrorResponse(
httpStatus.value(),
exc.getMessage(),
System.currentTimeMillis());

return new ResponseEntity<>(error, httpStatus);
}

@ExceptionHandler
public ResponseEntity<ProjectErrorResponse> handleException(ProjectInvalidFormatException exc) {
ProjectErrorResponse error = new ProjectErrorResponse();

public ResponseEntity<VolodbErrorResponse> handleException(ProjectInvalidFormatException exc) {
HttpStatus httpStatus = HttpStatus.BAD_REQUEST;

error.setStatus(httpStatus.value());
error.setMessage(exc.getMessage());
error.setTimeStamp(System.currentTimeMillis());
VolodbErrorResponse error = new VolodbErrorResponse(
httpStatus.value(),
exc.getMessage(),
System.currentTimeMillis());

return new ResponseEntity<>(error, httpStatus);
}

@ExceptionHandler
public ResponseEntity<ProjectErrorResponse> handleException(CountryNotFoundException exc) {
ProjectErrorResponse error = new ProjectErrorResponse();

public ResponseEntity<VolodbErrorResponse> handleException(CountryNotFoundException exc) {
HttpStatus httpStatus = HttpStatus.NOT_FOUND;

error.setStatus(httpStatus.value());
error.setMessage(exc.getMessage());
error.setTimeStamp(System.currentTimeMillis());
VolodbErrorResponse error = new VolodbErrorResponse(
httpStatus.value(),
exc.getMessage(),
System.currentTimeMillis());

return new ResponseEntity<>(error, httpStatus);
}

@ExceptionHandler
public ResponseEntity<ProjectErrorResponse> handleException(RuntimeException exc) {
ProjectErrorResponse error = new ProjectErrorResponse();

public ResponseEntity<VolodbErrorResponse> handleException(RuntimeException exc) {
HttpStatus httpStatus = HttpStatus.BAD_REQUEST;

error.setStatus(httpStatus.value());
error.setMessage(exc.getMessage());
error.setTimeStamp(System.currentTimeMillis());
VolodbErrorResponse error = new VolodbErrorResponse(
httpStatus.value(),
exc.getMessage(),
System.currentTimeMillis());

return new ResponseEntity<>(error, httpStatus);
}
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/dev/urner/volodb/rest/UserRestController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package dev.urner.volodb.rest;

import org.springframework.web.bind.annotation.RestController;

import dev.urner.volodb.entity.User;
import dev.urner.volodb.entity.UserNotFoundException;
import dev.urner.volodb.security.UserPrincipal;
import dev.urner.volodb.service.UserService;
import lombok.RequiredArgsConstructor;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserRestController {

private final UserService userService;

@GetMapping()
public User getUser(@AuthenticationPrincipal UserPrincipal principal) {
User theUser = userService.findByUsername(principal.getUsername());

if (theUser == null) {
throw new UserNotFoundException("Username not found - " + principal.getUsername());
}

return theUser;
}

@ExceptionHandler
public ResponseEntity<VolodbErrorResponse> handleException(UserNotFoundException exc) {
HttpStatus httpStatus = HttpStatus.NOT_FOUND;

VolodbErrorResponse error = new VolodbErrorResponse(
httpStatus.value(),
exc.getMessage(),
System.currentTimeMillis());

return new ResponseEntity<>(error, httpStatus);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
@RequiredArgsConstructor
@Getter
@Setter
public class AuthErrorResponse {
public class VolodbErrorResponse {
private final int status;
private final String message;
private final long timeStamp;
Expand Down
16 changes: 0 additions & 16 deletions src/main/java/dev/urner/volodb/rest/VolunteerErrorResponse.java

This file was deleted.

Loading

0 comments on commit 4ff64ad

Please sign in to comment.