-
Notifications
You must be signed in to change notification settings - Fork 60
added user entity and service methods #79
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
Open
prishagarg
wants to merge
3
commits into
Ayush0316:main
Choose a base branch
from
prishagarg:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
52 changes: 52 additions & 0 deletions
52
app(backend)/src/main/java/com/knockoutzone/backend/controller/UserController.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,52 @@ | ||
| package com.knockoutzone.backend.controller; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.DeleteMapping; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.PathVariable; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.PutMapping; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| import com.knockoutzone.backend.entity.User; | ||
| import com.knockoutzone.backend.service.UserService; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/api/users") | ||
| @RequiredArgsConstructor | ||
| public class UserController { | ||
|
|
||
| private final UserService userService; | ||
|
|
||
| @PostMapping | ||
| public ResponseEntity<User> createUser(@RequestBody User user) { | ||
| return ResponseEntity.ok(userService.createUser(user)); | ||
| } | ||
|
|
||
| @GetMapping("/{id}") | ||
| public ResponseEntity<User> getUser(@PathVariable Long id) { | ||
| return ResponseEntity.ok(userService.getUserById(id)); | ||
| } | ||
|
|
||
| @GetMapping | ||
| public ResponseEntity<List<User>> getAllUsers() { | ||
| return ResponseEntity.ok(userService.getAllUsers()); | ||
| } | ||
|
|
||
| @PutMapping("/{id}") | ||
| public ResponseEntity<User> updateUser(@PathVariable Long id, @RequestBody User user) { | ||
| return ResponseEntity.ok(userService.updateUser(id, user)); | ||
| } | ||
|
|
||
| @DeleteMapping("/{id}") | ||
| public ResponseEntity<Void> deleteUser(@PathVariable Long id) { | ||
| userService.deleteUser(id); | ||
| return ResponseEntity.noContent().build(); | ||
| } | ||
| } | ||
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
65 changes: 65 additions & 0 deletions
65
app(backend)/src/main/java/com/knockoutzone/backend/entity/User.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,65 @@ | ||
| package com.knockoutzone.backend.entity; | ||
|
|
||
|
|
||
| import org.hibernate.envers.Audited; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonIgnore; | ||
|
|
||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.GeneratedValue; | ||
| import jakarta.persistence.GenerationType; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.Table; | ||
| import jakarta.persistence.OneToMany; | ||
| import jakarta.persistence.CascadeType; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonManagedReference; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import lombok.Setter; | ||
|
|
||
|
|
||
| @Entity | ||
| @Getter | ||
| @Setter | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| @Builder | ||
| @Audited(withModifiedFlag = true) | ||
| @Table(name="users") | ||
| public class User extends AuditingEntity { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.AUTO) | ||
| private Long id; | ||
|
|
||
| private String username; | ||
|
|
||
| private String email; | ||
|
|
||
| @JsonIgnore | ||
| private String password; | ||
|
|
||
| private String firstName; | ||
|
|
||
| private String lastName; | ||
|
|
||
| private String phoneNumber; | ||
|
|
||
| private String profilePictureUrl; | ||
|
|
||
| private Boolean isEmailVerified; | ||
|
|
||
| private Boolean isActive; | ||
|
|
||
| @OneToMany(mappedBy = "host", cascade = CascadeType.ALL, orphanRemoval = true) | ||
| @JsonManagedReference | ||
| private List<Tournament> tournamentsHosted = new ArrayList<>(); | ||
|
|
||
| } |
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 |
|---|---|---|
|
|
@@ -6,4 +6,6 @@ public enum EventMode { | |
| DUO, | ||
| SQUAD, | ||
| TEAM | ||
|
|
||
| } | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -5,3 +5,4 @@ public enum Platform { | |
| OFFLINE, | ||
| HYBRID | ||
| } | ||
|
|
||
20 changes: 20 additions & 0 deletions
20
app(backend)/src/main/java/com/knockoutzone/backend/repository/UserRepository.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 com.knockoutzone.backend.repository; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| import com.knockoutzone.backend.entity.User; | ||
|
|
||
| @Repository | ||
| public interface UserRepository extends JpaRepository<User, Long> { | ||
|
|
||
| Optional<User> findByEmail(String email); | ||
|
|
||
| Optional<User> findByUsername(String username); | ||
|
|
||
| Boolean existsByEmail(String email); | ||
|
|
||
| Boolean existsByUsername(String username); | ||
| } |
59 changes: 59 additions & 0 deletions
59
app(backend)/src/main/java/com/knockoutzone/backend/service/UserService.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,59 @@ | ||
| package com.knockoutzone.backend.service; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import com.knockoutzone.backend.entity.User; | ||
| import com.knockoutzone.backend.repository.UserRepository; | ||
|
|
||
| import jakarta.persistence.EntityNotFoundException; | ||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class UserService { | ||
|
|
||
| private final UserRepository userRepository; | ||
|
|
||
| public User createUser(User user) { | ||
| return userRepository.save(user); | ||
| } | ||
|
|
||
| public User getUserById(Long id) { | ||
| return userRepository.findById(id) | ||
| .orElseThrow(() -> new EntityNotFoundException("User not found with id: " + id)); | ||
| } | ||
|
|
||
| public List<User> getAllUsers() { | ||
| return userRepository.findAll(); | ||
| } | ||
|
|
||
| public User updateUser(Long id, User updatedUser) { | ||
| User existingUser = getUserById(id); | ||
| existingUser.setUsername(updatedUser.getUsername()); | ||
| existingUser.setEmail(updatedUser.getEmail()); | ||
| existingUser.setFirstName(updatedUser.getFirstName()); | ||
| existingUser.setLastName(updatedUser.getLastName()); | ||
| existingUser.setPhoneNumber(updatedUser.getPhoneNumber()); | ||
| existingUser.setProfilePictureUrl(updatedUser.getProfilePictureUrl()); | ||
| existingUser.setIsActive(updatedUser.getIsActive()); | ||
|
|
||
| return userRepository.save(existingUser); | ||
| } | ||
|
|
||
| public void deleteUser(Long id) { | ||
| if (!userRepository.existsById(id)) { | ||
| throw new EntityNotFoundException("User not found with id: " + id); | ||
| } | ||
| userRepository.deleteById(id); | ||
| } | ||
|
|
||
| public boolean existsByEmail(String email) { | ||
| return userRepository.existsByEmail(email); | ||
| } | ||
|
|
||
| public boolean existsByUsername(String username) { | ||
| return userRepository.existsByUsername(username); | ||
| } | ||
| } |
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.
We have our custom response dto, you have to use that.