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

Feature: Add GET User by email endpoint #109

Merged
merged 2 commits into from
Sep 19, 2024
Merged
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
46 changes: 42 additions & 4 deletions postman_collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -644,15 +644,15 @@
"header": [],
"body": {
"mode": "raw",
"raw": "{\r\n \"firstName\" : \"Doktor\",\r\n \"lastName\" : \"Nikolic\",\r\n \"email\" : \"jozsef.nikolic@gmail.com\",\r\n \"userId\" : \"152\"\r\n}",
"raw": "{\r\n \"firstName\": \"Ana\",\r\n \"lastName\": \"Tot-Bagi\",\r\n \"email\": \"hunortotbagi@gmail.com\"\r\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "http://localhost:8080/api/v1/parents/402",
"raw": "http://localhost:8080/api/v1/parents/352",
"protocol": "http",
"host": [
"localhost"
Expand All @@ -662,7 +662,7 @@
"api",
"v1",
"parents",
"402"
"352"
]
}
},
Expand Down Expand Up @@ -1797,7 +1797,7 @@
"response": []
},
{
"name": "/api/v1/users/{id}",
"name": "/api/v1/users/{userId}",
"request": {
"auth": {
"type": "basic",
Expand Down Expand Up @@ -1833,6 +1833,44 @@
},
"response": []
},
{
"name": "/api/v1/users/email/{userEmail}",
"request": {
"auth": {
"type": "basic",
"basic": [
{
"key": "password",
"value": "pass",
"type": "string"
},
{
"key": "username",
"value": "[email protected]",
"type": "string"
}
]
},
"method": "GET",
"header": [],
"url": {
"raw": "http://localhost:8080/api/v1/users/email/[email protected]",
"protocol": "http",
"host": [
"localhost"
],
"port": "8080",
"path": [
"api",
"v1",
"users",
"email",
"[email protected]"
]
}
},
"response": []
},
{
"name": "/api/v1/users/{id}",
"request": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public ResponseEntity<Iterable<UserEntity>> getAllUsers() {
public ResponseEntity<UserEntity> getUserById(@PathVariable Integer userId) {
return new ResponseEntity<>(userService.getUserById(userId), HttpStatus.OK);
}

@GetMapping("/email/{userEmail}")
public ResponseEntity<UserEntity> getUserByEmail(@PathVariable String userEmail) {
return new ResponseEntity<>(userService.getUserByEmail(userEmail), HttpStatus.OK);
}

@PutMapping("/{userId}")
public ResponseEntity<UserEntity> updateUser(@PathVariable Integer userId, @RequestBody UserRequestDTO userDTOBody) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.electric_diary.repositories;

import java.util.Optional;

import org.springframework.data.repository.CrudRepository;

import com.electric_diary.entities.UserEntity;

public interface UserRepository extends CrudRepository<UserEntity, Integer> {

Optional<UserEntity> findByEmail(String userEmail);
}
2 changes: 2 additions & 0 deletions src/main/java/com/electric_diary/services/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public interface UserService {
public Iterable<UserEntity> getAllUsers();

public UserEntity getUserById(Integer userId);

public UserEntity getUserByEmail(String userEmail);

public UserEntity updateUser(Integer userId, UserRequestDTO userDTOBody);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public UserEntity getUserById(Integer userId) {
logger.info("Fetched user with ID {}.", userId);
return userRepository.findById(userId).orElseThrow(() -> new NotFoundException("User", userId));
}

@Override
public UserEntity getUserByEmail(String userEmail) {
return userRepository.findByEmail(userEmail).orElseThrow(() -> new NotFoundException("User", 1));
}

@Override
public UserEntity updateUser(Integer userId, UserRequestDTO userDTOBody) {
Expand Down