Skip to content
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

feat: get all #6

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 56 additions & 55 deletions src/main/java/com/nkubito/ehospital/controllers/UserController.java
Original file line number Diff line number Diff line change
@@ -1,66 +1,67 @@
package com.nkubito.ehospital.controllers;

import com.nkubito.ehospital.models.Role;
import com.nkubito.ehospital.store.Users;
import com.nkubito.ehospital.utils.Response;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nkubito.ehospital.store.Users;
import com.nkubito.ehospital.utils.Response;
import org.springframework.http.HttpStatus;
import java.time.LocalDateTime;

@RestController
public class UserController {
@GetMapping(path = "/api/users")
public ResponseEntity<Response> getUsers() {
return ResponseEntity.ok(
Response.builder()
.timeStamp(LocalDateTime.now())
.message("All users retreived successfully")
.status(HttpStatus.OK)
.statusCode(HttpStatus.OK.value())
.data(Users.INSTANCE.retreiveUser())
.build()
);
}
@GetMapping(path = "/api/users/physicians")
public ResponseEntity<Response> getAllPhysicians() {
return ResponseEntity.ok(
Response.builder()
.timeStamp(LocalDateTime.now())
.message("Retreive all Physicians")
.status(HttpStatus.OK)
.statusCode(HttpStatus.OK.value())
.data(Users.INSTANCE.getUserByRole("Physician"))
.build()
);
}
@GetMapping(path = "/api/users/patients")
public ResponseEntity<Response> getAllPatients() {
return ResponseEntity.ok(
Response.builder()
.timeStamp(LocalDateTime.now())
.message("Retreive all Patients")
.status(HttpStatus.OK)
.statusCode(HttpStatus.OK.value())
.data(Users.INSTANCE.getUserByRole("Patient"))
.build()
);
}
@GetMapping(path = "/api/users/pharmacists")
public ResponseEntity<Response> getAllPharmacists() {
return ResponseEntity.ok(
Response.builder()
.timeStamp(LocalDateTime.now())
.message("Retreive all Pharmacists")
.status(HttpStatus.OK)
.statusCode(HttpStatus.OK.value())
.data(Users.INSTANCE.getUserByRole("Pharmacist"))
.build()
);
}
@GetMapping(path = "/api/users")
public ResponseEntity<Response> getUsers() {
return ResponseEntity.ok(
Response.builder()
.timeStamp(LocalDateTime.now())
.message("All users retreived successfully")
.status(HttpStatus.OK)
.statusCode(HttpStatus.OK.value())
.data(Users.INSTANCE.retreiveUser())
.build()
);
}

@GetMapping(path = "/api/users/physicians")
public ResponseEntity<Response> getAllPhysicians() {
return ResponseEntity.ok(
Response.builder()
.timeStamp(LocalDateTime.now())
.message("Retreive all Physicians")
.status(HttpStatus.OK)
.statusCode(HttpStatus.OK.value())
.data(Users.INSTANCE.getUserByRole(Role.Physician))
.build()
);
}

@GetMapping(path = "/api/users/patients")
public ResponseEntity<Response> getAllPatients() {
return ResponseEntity.ok(
Response.builder()
.timeStamp(LocalDateTime.now())
.message("Retreive all Patients")
.status(HttpStatus.OK)
.statusCode(HttpStatus.OK.value())
.data(Users.INSTANCE.getUserByRole(Role.Patient))
.build()
);
}

@GetMapping(path = "/api/users/pharmacists")
public ResponseEntity<Response> getAllPharmacists() {
return ResponseEntity.ok(
Response.builder()
.timeStamp(LocalDateTime.now())
.message("Retreive all Pharmacists")
.status(HttpStatus.OK)
.statusCode(HttpStatus.OK.value())
.data(Users.INSTANCE.getUserByRole(Role.Pharmacist))
.build()
Comment on lines +61 to +63
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what I change

);
}

}
27 changes: 10 additions & 17 deletions src/main/java/com/nkubito/ehospital/store/Users.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,15 @@ public String getKey(User user) {

private boolean contains(User user) {
return switch (user.getRole()) {
case Physician -> users.keySet().stream().anyMatch(u -> u.equals(user.getEmail()));
case Pharmacist -> users.keySet().stream().anyMatch(u -> u.equals(user.getPhoneNumber()));
case Patient -> users.keySet().stream().anyMatch(u -> u.equals(user.getUsername()));
case Physician ->
users.keySet().stream().anyMatch(u -> u.equals(user.getEmail()));
case Pharmacist ->
users.keySet().stream().anyMatch(u -> u.equals(user.getPhoneNumber()));
case Patient ->
users.keySet().stream().anyMatch(u -> u.equals(user.getUsername()));
};
}

public List<User> retreiveUser() {
List<User> userList = new ArrayList<>();
Map<String, User> userMap = Users.INSTANCE.getUsers();
Expand All @@ -54,19 +58,8 @@ public List<User> retreiveUser() {
return userList;
}

public List<User> getUserByRole(String role) {
List<User> filteredUsers = new ArrayList<>();
Map<String, User> userMap = Users.INSTANCE.getUsers();

for (Map.Entry<String, User> entry : userMap.entrySet()) {
User user = entry.getValue();

if (user.getRole().toString().equalsIgnoreCase(role)) {
filteredUsers.add(user);
}
}

return filteredUsers;
public List<User> getUserByRole(Role role) {
return users.values().stream().filter(user -> user.getRole().equals(role)).toList();
}
Comment on lines -69 to 63
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changes


}