Skip to content
This repository was archived by the owner on Jan 13, 2023. It is now read-only.

Commit 9d676ee

Browse files
committed
Add new exercise "User Service"
1 parent 2491af0 commit 9d676ee

File tree

11 files changed

+380
-1
lines changed

11 files changed

+380
-1
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<module>spring-data-jpa-exercises-model</module>
1212
<module>spring-data-jpa-exercises-util</module>
1313
<module>hello-jpa-repository</module>
14+
<module>user-service</module>
1415
</modules>
1516

1617
<packaging>pom</packaging>

spring-data-jpa-exercises-model/src/main/java/com/bobocode/model/User.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ public class User {
3636
@Column(name = "creation_date")
3737
private LocalDate creationDate;
3838

39-
4039
@OneToOne(mappedBy = "user", cascade = CascadeType.ALL)
4140
private Address address;
4241

user-service/README.MD

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# <img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/image/logo_transparent_background.png" height=50/>Hello JpaRepository exercise :muscle:
2+
Improve your *Spring Data JPA* Java configuration skills
3+
### Task
4+
The task is to **implement `UserService` using `UserRepository`**. In order to do that, you need to **provide all required
5+
configuration, implement methods, and create custom repository**. Please follow the instructions in the *todo* section.
6+
7+
To verify your configuration, run `UserServiceAppTest.java`
8+
9+
10+
### Pre-conditions :heavy_exclamation_mark:
11+
You're supposed to be familiar with *Spring IoC* and *Dependency injection*
12+
13+
### How to start :question:
14+
* Just clone the repository and start implementing the **todo** section, verify your changes by running tests
15+
* If you don't have enough knowledge about this domain, check out the [links below](#related-materials-information_source)
16+
* Don't worry if you got stuck, checkout the **exercise/completed** branch and see the final implementation
17+
18+
### Related materials :information_source:
19+
* [Spring Data JPA basics tutorial](https://github.com/bobocode-projects/spring-data-jpa-tutorial/tree/master/jpa-repository-basics)<img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/image/logo_transparent_background.png" height=20/>
20+

user-service/pom.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>spring-data-jpa-exercises</artifactId>
7+
<groupId>com.bobocode</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>user-service</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>com.bobocode</groupId>
17+
<artifactId>spring-data-jpa-exercises-util</artifactId>
18+
<version>1.0-SNAPSHOT</version>
19+
</dependency>
20+
</dependencies>
21+
22+
</project>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.bobocode.config;
2+
3+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
4+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
5+
import org.springframework.orm.jpa.JpaVendorAdapter;
6+
7+
import javax.sql.DataSource;
8+
9+
/**
10+
* This class provides spring configuration for {@link javax.persistence.EntityManagerFactory} bean.
11+
* <p>
12+
* todo: 0. PLEASE NOTE, THAT SOME REQUIRED STEPS ARE OMITTED IN THE TODO LIST AND YOU HAVE TO DO IT ON YOUR OWN
13+
* <p>
14+
* todo: 1. Configure {@link DataSource} bean
15+
* todo: 2. Configure {@link JpaVendorAdapter} bean
16+
* todo: 3. Configure {@link javax.persistence.EntityManagerFactory} bean with name "entityManagerFactory"
17+
* todo: 4. Enable JPA repository
18+
*/
19+
public class JpaConfig {
20+
21+
public DataSource dataSource() {
22+
return new EmbeddedDatabaseBuilder()
23+
.setType(EmbeddedDatabaseType.H2)
24+
.build();
25+
}
26+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.bobocode.config;
2+
3+
import org.springframework.transaction.PlatformTransactionManager;
4+
5+
/**
6+
* This class is provides root Java config for Spring application.
7+
* <p>
8+
* todo: 0. PLEASE NOTE, THAT SOME REQUIRED STEPS ARE OMITTED IN THE TODO LIST AND YOU HAVE TO DO IT ON YOUR OWN
9+
* <p>
10+
* todo: 1. Configure {@link PlatformTransactionManager} bean with name "transactionManager"
11+
* todo: 2. Enable transaction management
12+
*/
13+
public class RootConfig {
14+
}
15+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.bobocode.dao;
2+
3+
import com.bobocode.model.RoleType;
4+
5+
/**
6+
* This class declares custom {@link UserRepository} methods
7+
* <p>
8+
* todo: 0. PLEASE NOTE, THAT SOME REQUIRED STEPS ARE OMITTED IN THE TODO LIST AND YOU HAVE TO DO IT ON YOUR OWN
9+
* <p>
10+
* todo: 1. Create class called "CustomUserRepositoryImpl"
11+
* todo: 2. Mark that class {@link org.springframework.stereotype.Repository}
12+
* todo: 3. Mark that class {@link org.springframework.transaction.annotation.Transactional}
13+
* todo: 4. Implement method {@link CustomUserRepository#addRoleToAllUsers(RoleType)}
14+
*/
15+
public interface CustomUserRepository {
16+
void addRoleToAllUsers(RoleType roleType);
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.bobocode.dao;
2+
3+
import com.bobocode.model.User;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
/**
7+
* This interface represents a data access object (DAO) for {@link User}.
8+
* <p>
9+
* todo: 0. PLEASE NOTE, THAT SOME REQUIRED STEPS ARE OMITTED IN THE TODO LIST AND YOU HAVE TO DO IT ON YOUR OWN
10+
* <p>
11+
* todo: 1. Configure {@link UserRepository} as {@link JpaRepository} for class User
12+
* todo: 2. Create method that finds a list of Users by address city using Spring Data method name convention
13+
* todo: 3. Create method that finds optional user by email fetching its address and roles using {@link org.springframework.data.jpa.repository.Query}
14+
* todo: 4. Add custom User repository interface
15+
*/
16+
public interface UserRepository extends JpaRepository{
17+
18+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.bobocode.exception;
2+
3+
public class EntityNotFoundException extends RuntimeException{
4+
public EntityNotFoundException(String message) {
5+
super(message);
6+
}
7+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.bobocode.service;
2+
3+
import com.bobocode.dao.UserRepository;
4+
import com.bobocode.exception.EntityNotFoundException;
5+
import com.bobocode.model.RoleType;
6+
import com.bobocode.model.User;
7+
8+
import java.util.List;
9+
10+
/**
11+
* This class provides service logic for {@link User}.
12+
* <p>
13+
* todo: 0. PLEASE NOTE, THAT SOME REQUIRED STEPS ARE OMITTED IN THE TODO LIST AND YOU HAVE TO DO IT ON YOUR OWN
14+
* <p>
15+
* todo: 1. Implement {@link UserService#findByCity(String)} using {@link UserRepository}, make method read only
16+
* todo: 2. Implement {@link UserService#getByEmail(String)} using {@link UserRepository}, make method read only
17+
* todo: 3. In case user is not found by email, throw {@link EntityNotFoundException} with message "Cannot find user by email ${email}"
18+
* todo: 4. Implement {@link UserService#addRoleToAllUser(RoleType)} using {@link UserRepository}
19+
*/
20+
public class UserService {
21+
public List<User> findByCity(String city) {
22+
throw new UnsupportedOperationException("Do your best and implement this method!");
23+
}
24+
25+
public User getByEmail(String email) {
26+
throw new UnsupportedOperationException("Do your best and implement this method!");
27+
}
28+
29+
public void addRoleToAllUser(RoleType roleType) {
30+
throw new UnsupportedOperationException("Do your best and implement this method!");
31+
}
32+
}

0 commit comments

Comments
 (0)