From 81c4302a9aa4c1390a38fad0f2bf0cfd0fb61415 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 21 Dec 2023 23:50:40 +0100 Subject: [PATCH 1/4] Changed @RequestBody to @RequestParam in PUT method --- src/main/java/com/MeetMate/roles/Roles.java | 2 +- src/main/java/com/MeetMate/user/UserController.java | 10 +++++----- src/main/java/com/MeetMate/user/UserService.java | 3 ++- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/MeetMate/roles/Roles.java b/src/main/java/com/MeetMate/roles/Roles.java index f91905f..ba719cc 100644 --- a/src/main/java/com/MeetMate/roles/Roles.java +++ b/src/main/java/com/MeetMate/roles/Roles.java @@ -5,4 +5,4 @@ public enum Roles { OWNER, ADMIN, CUSTOMER; -} +} \ No newline at end of file diff --git a/src/main/java/com/MeetMate/user/UserController.java b/src/main/java/com/MeetMate/user/UserController.java index c6216c7..58e4f98 100644 --- a/src/main/java/com/MeetMate/user/UserController.java +++ b/src/main/java/com/MeetMate/user/UserController.java @@ -1,14 +1,14 @@ package com.MeetMate.user; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.StreamingHttpOutputMessage; -import org.springframework.util.LinkedMultiValueMap; +import org.springframework.http.MediaType; import org.springframework.util.MultiValueMap; import org.springframework.web.bind.annotation.*; -import org.springframework.web.service.annotation.PutExchange; import java.util.List; +import static org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED; + @RestController @RequestMapping(path = "api/test/user") public class UserController { @@ -35,8 +35,8 @@ public void registerNewUser(@RequestBody MultiValueMap formData) userService.addNewUser(formData); } - @PostMapping(path = "put") - public void updateUser(@RequestBody MultiValueMap formData) { + @PutMapping(path = "put") + public void updateUser(@RequestParam MultiValueMap formData) { System.out.println(formData); userService.updateUser(formData); } diff --git a/src/main/java/com/MeetMate/user/UserService.java b/src/main/java/com/MeetMate/user/UserService.java index 02af93f..13192a3 100644 --- a/src/main/java/com/MeetMate/user/UserService.java +++ b/src/main/java/com/MeetMate/user/UserService.java @@ -21,6 +21,7 @@ public UserService(UserRepository userRepository) { public User getUser(Long userId) { Optional userOptional = userRepository.findUserById(userId); + // ??? userRepository.findUserById(userId); if (userOptional.isPresent()) { return userOptional.get(); @@ -70,7 +71,7 @@ public void updateUser(MultiValueMap data) { if (userRepository.findUserByEmail(email).isEmpty()) { user.setEmail(email); - } + } // throw error user.setPassword(password); } From aa228012fccbc005388dc092abaf9201caf2d143 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 22 Dec 2023 00:00:37 +0100 Subject: [PATCH 2/4] Changed POST to RequestParam to match PUT and conventional standards --- src/main/java/com/MeetMate/user/UserController.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/main/java/com/MeetMate/user/UserController.java b/src/main/java/com/MeetMate/user/UserController.java index 58e4f98..7bd6acb 100644 --- a/src/main/java/com/MeetMate/user/UserController.java +++ b/src/main/java/com/MeetMate/user/UserController.java @@ -1,14 +1,11 @@ package com.MeetMate.user; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.MediaType; import org.springframework.util.MultiValueMap; import org.springframework.web.bind.annotation.*; import java.util.List; -import static org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED; - @RestController @RequestMapping(path = "api/test/user") public class UserController { @@ -31,7 +28,7 @@ public List getAllUsers() { } @PostMapping(path = "post") - public void registerNewUser(@RequestBody MultiValueMap formData) { + public void registerNewUser(@RequestParam MultiValueMap formData) { userService.addNewUser(formData); } From 0352a5b63b45975052bfd4e9bea60ffc9ecf155c Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 22 Dec 2023 22:47:10 +0100 Subject: [PATCH 3/4] no relevant changes --- src/main/java/com/MeetMate/user/UserController.java | 2 ++ src/main/java/com/MeetMate/user/UserService.java | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/main/java/com/MeetMate/user/UserController.java b/src/main/java/com/MeetMate/user/UserController.java index 7bd6acb..839c3b9 100644 --- a/src/main/java/com/MeetMate/user/UserController.java +++ b/src/main/java/com/MeetMate/user/UserController.java @@ -18,11 +18,13 @@ public UserController(UserService userService) { } @GetMapping(path = "get") + @ResponseBody public User getUser(@RequestParam(name = "id") Long userId) { return userService.getUser(userId); } @GetMapping(path = "getAll") + @ResponseBody public List getAllUsers() { return userService.getAllUsers(); } diff --git a/src/main/java/com/MeetMate/user/UserService.java b/src/main/java/com/MeetMate/user/UserService.java index 13192a3..7331d3f 100644 --- a/src/main/java/com/MeetMate/user/UserService.java +++ b/src/main/java/com/MeetMate/user/UserService.java @@ -6,9 +6,11 @@ import org.springframework.stereotype.Service; import org.springframework.util.MultiValueMap; +import java.net.HttpURLConnection; import java.util.List; import java.util.Optional; + @Service public class UserService { From 308ac675c8dc01f33e457d3e3280fd7fe85cbd20 Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 23 Dec 2023 00:56:00 +0100 Subject: [PATCH 4/4] added lombok to simplify code --- pom.xml | 29 ++++++-- .../{Main.java => MeetMateApplication.java} | 4 +- src/main/java/com/MeetMate/roles/Role.java | 8 +++ src/main/java/com/MeetMate/roles/Roles.java | 8 --- src/main/java/com/MeetMate/user/User.java | 68 ++----------------- .../java/com/MeetMate/user/UserService.java | 3 - .../MeetMate/MeetMateApplicationTests.java | 17 ++++- 7 files changed, 55 insertions(+), 82 deletions(-) rename src/main/java/com/MeetMate/{Main.java => MeetMateApplication.java} (82%) create mode 100644 src/main/java/com/MeetMate/roles/Role.java delete mode 100644 src/main/java/com/MeetMate/roles/Roles.java diff --git a/pom.xml b/pom.xml index 9850069..fa3244e 100644 --- a/pom.xml +++ b/pom.xml @@ -16,15 +16,18 @@ 21 + org.springframework.boot spring-boot-starter-data-jpa - - - - + + + org.springframework.boot + spring-boot-starter-security + + org.springframework.boot spring-boot-starter-web @@ -35,23 +38,39 @@ postgresql runtime + org.springframework.boot spring-boot-starter-test test + org.springframework.security spring-security-test test + org.jetbrains annotations RELEASE compile - + + + org.springframework.boot + spring-boot-devtools + true + + + + org.projectlombok + lombok + 1.18.30 + provided + + diff --git a/src/main/java/com/MeetMate/Main.java b/src/main/java/com/MeetMate/MeetMateApplication.java similarity index 82% rename from src/main/java/com/MeetMate/Main.java rename to src/main/java/com/MeetMate/MeetMateApplication.java index c99df0e..7e18f53 100644 --- a/src/main/java/com/MeetMate/Main.java +++ b/src/main/java/com/MeetMate/MeetMateApplication.java @@ -8,9 +8,9 @@ @SpringBootApplication //@EnableConfigurationProperties //@EntityScan(basePackages = {"com.MeetMate.user"}) //force scan the packages -public class Main { +public class MeetMateApplication { public static void main(String[] args) { - SpringApplication.run(Main.class, args); + SpringApplication.run(MeetMateApplication.class, args); } } diff --git a/src/main/java/com/MeetMate/roles/Role.java b/src/main/java/com/MeetMate/roles/Role.java new file mode 100644 index 0000000..b12cdb4 --- /dev/null +++ b/src/main/java/com/MeetMate/roles/Role.java @@ -0,0 +1,8 @@ +package com.MeetMate.roles; + +public enum Role { + + ADMIN, + COMPANY, + CLIENT; +} \ No newline at end of file diff --git a/src/main/java/com/MeetMate/roles/Roles.java b/src/main/java/com/MeetMate/roles/Roles.java deleted file mode 100644 index ba719cc..0000000 --- a/src/main/java/com/MeetMate/roles/Roles.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.MeetMate.roles; - -public enum Roles { - - OWNER, - ADMIN, - CUSTOMER; -} \ No newline at end of file diff --git a/src/main/java/com/MeetMate/user/User.java b/src/main/java/com/MeetMate/user/User.java index 066f085..8f7720f 100644 --- a/src/main/java/com/MeetMate/user/User.java +++ b/src/main/java/com/MeetMate/user/User.java @@ -1,6 +1,8 @@ package com.MeetMate.user; +import com.MeetMate.roles.Role; import jakarta.persistence.*; +import lombok.*; import java.time.LocalDate; import java.time.Month; @@ -8,6 +10,7 @@ @Entity @Table(name = "users") +@Data public class User { @Id @SequenceGenerator( @@ -22,16 +25,17 @@ public class User { private Long id; private String name; private LocalDate birthday; + @Setter(AccessLevel.NONE) private LocalDate createdAt; private String email; private String password; - //enum Rolle + private Role role; //Last login //(refresh token) //bool verified - - //No need for column in database @Transient + @Getter(AccessLevel.NONE) + @Setter(AccessLevel.NONE) private int age; public User() { @@ -62,66 +66,8 @@ public User(String email, String password) { this.password = password; } - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public LocalDate getBirthday() { - if (this.birthday == null) birthday = LocalDate.EPOCH; - return birthday; - } - - public void setBirthday(LocalDate birthday) { - this.birthday = birthday; - } - - public String getEmail() { - return email; - } - - public void setEmail(String email) { - this.email = email; - } - - public String getPassword() { - return password; - } - - public void setPassword(String password) { - this.password = password; - } - public int getAge() { return Period.between(getBirthday(), LocalDate.now()).getYears(); } - public LocalDate getCreatedAt() { - return createdAt; - } - - @Override - public String toString() { - return "User{" + - "id=" + id + - ", name='" + name + '\'' + - ", birthday=" + birthday + - ", createdAt=" + createdAt + - ", email='" + email + '\'' + - ", password='" + password + '\'' + - ", age=" + age + - '}'; - } - } diff --git a/src/main/java/com/MeetMate/user/UserService.java b/src/main/java/com/MeetMate/user/UserService.java index 7331d3f..6453a27 100644 --- a/src/main/java/com/MeetMate/user/UserService.java +++ b/src/main/java/com/MeetMate/user/UserService.java @@ -1,16 +1,13 @@ package com.MeetMate.user; import jakarta.transaction.Transactional; -import org.jetbrains.annotations.NotNull; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.MultiValueMap; -import java.net.HttpURLConnection; import java.util.List; import java.util.Optional; - @Service public class UserService { diff --git a/src/test/java/com/MeetMate/MeetMateApplicationTests.java b/src/test/java/com/MeetMate/MeetMateApplicationTests.java index c77f80d..71a4ff7 100644 --- a/src/test/java/com/MeetMate/MeetMateApplicationTests.java +++ b/src/test/java/com/MeetMate/MeetMateApplicationTests.java @@ -1,13 +1,24 @@ package com.MeetMate; +import com.MeetMate.user.User; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; +import java.time.LocalDate; +import java.time.Month; + @SpringBootTest class MeetMateApplicationTests { - @Test - void contextLoads() { - } + @Test + void contextLoads() { + User test = new User( + "Carl A", + LocalDate.of(2000, Month.JANUARY, 21), + "carl.A@gmail.com", + "carl123"); + System.out.println(test.getAge()); + System.out.println(test.getName()); + } }