Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
U
userRoleHandler
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Aleksey
userRoleHandler
Commits
a9b217a7
Commit
a9b217a7
authored
Jan 20, 2021
by
Alex
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
new services
parent
fa0f6142
Changes
17
Hide whitespace changes
Inline
Side-by-side
Showing
17 changed files
with
281 additions
and
70 deletions
+281
-70
pom.xml
pom.xml
+43
-19
UserRoleHandlerApplication.java
src/main/java/com/yagudza/UserRoleHandlerApplication.java
+0
-6
WebInitializer.java
src/main/java/com/yagudza/WebInitializer.java
+17
-0
RoleController.java
src/main/java/com/yagudza/controller/RoleController.java
+23
-0
UserController.java
src/main/java/com/yagudza/controller/UserController.java
+8
-3
Role.java
src/main/java/com/yagudza/domain/Role.java
+14
-7
User.java
src/main/java/com/yagudza/domain/User.java
+10
-9
RoleDto.java
src/main/java/com/yagudza/dto/RoleDto.java
+22
-0
UserDto.java
src/main/java/com/yagudza/dto/UserDto.java
+4
-1
UserResponseDto.java
src/main/java/com/yagudza/dto/UserResponseDto.java
+36
-0
UserMapper.java
src/main/java/com/yagudza/mappers/UserMapper.java
+20
-0
UserRepository.java
src/main/java/com/yagudza/repository/UserRepository.java
+3
-1
RoleService.java
src/main/java/com/yagudza/servise/RoleService.java
+4
-0
RoleServiceImpl.java
src/main/java/com/yagudza/servise/RoleServiceImpl.java
+17
-1
UserService.java
src/main/java/com/yagudza/servise/UserService.java
+4
-5
UserServiceImpl.java
src/main/java/com/yagudza/servise/UserServiceImpl.java
+52
-16
application.properties
src/main/resources/application.properties
+4
-2
No files found.
pom.xml
View file @
a9b217a7
...
...
@@ -28,32 +28,40 @@
<artifactId>
spring-boot-starter-web
</artifactId>
</dependency>
<dependency>
<groupId>
com.h2database
</groupId>
<artifactId>
h2
</artifactId>
<scope>
runtime
</scope>
</dependency>
<dependency>
<groupId>
com.h2database
</groupId>
<artifactId>
h2
</artifactId>
<scope>
runtime
</scope>
</dependency>
<!-- <dependency>-->
<!-- <groupId>mysql</groupId>-->
<!-- <artifactId>mysql-connector-java</artifactId>-->
<!-- <version>8.0.21</version>-->
<!--
<groupId>mysql</groupId>-->
<!--
<artifactId>mysql-connector-java</artifactId>-->
<!--
<version>8.0.21</version>-->
<!-- </dependency>-->
<dependency>
<groupId>
org.modelmapper
</groupId>
<artifactId>
modelmapper
</artifactId>
<version>
0.7.5
</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-test</artifactId>-->
<!-- <scope>test</scope>-->
<!-- <exclusions>-->
<!-- <exclusion>-->
<!-- <groupId>org.junit.vintage</groupId>-->
<!-- <artifactId>junit-vintage-engine</artifactId>-->
<!-- </exclusion>-->
<!-- </exclusions>-->
<!-- </dependency>-->
<dependency>
<groupId>
org.mapstruct
</groupId>
<artifactId>
mapstruct
</artifactId>
<version>
1.4.1.Final
</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-test</artifactId>-->
<!-- <scope>test</scope>-->
<!-- <exclusions>-->
<!-- <exclusion>-->
<!-- <groupId>org.junit.vintage</groupId>-->
<!-- <artifactId>junit-vintage-engine</artifactId>-->
<!-- </exclusion>-->
<!-- </exclusions>-->
<!-- </dependency>-->
</dependencies>
<build>
...
...
@@ -62,6 +70,22 @@
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-maven-plugin
</artifactId>
</plugin>
<plugin>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-compiler-plugin
</artifactId>
<version>
3.8.1
</version>
<configuration>
<source>
11
</source>
<target>
11
</target>
<annotationProcessorPaths>
<path>
<groupId>
org.mapstruct
</groupId>
<artifactId>
mapstruct-processor
</artifactId>
<version>
1.4.1.Final
</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
...
...
src/main/java/com/yagudza/UserRoleHandlerApplication.java
View file @
a9b217a7
package
com
.
yagudza
;
import
org.modelmapper.ModelMapper
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
import
org.springframework.context.annotation.Bean
;
@SpringBootApplication
public
class
UserRoleHandlerApplication
{
...
...
@@ -12,8 +10,4 @@ public class UserRoleHandlerApplication {
SpringApplication
.
run
(
UserRoleHandlerApplication
.
class
,
args
);
}
@Bean
public
ModelMapper
modelMapper
()
{
return
new
ModelMapper
();
}
}
src/main/java/com/yagudza/WebInitializer.java
0 → 100644
View file @
a9b217a7
package
com
.
yagudza
;
import
org.springframework.boot.builder.SpringApplicationBuilder
;
import
org.springframework.boot.web.servlet.support.SpringBootServletInitializer
;
public
class
WebInitializer
extends
SpringBootServletInitializer
{
@Override
protected
SpringApplicationBuilder
configure
(
SpringApplicationBuilder
application
)
{
return
application
.
sources
(
UserRoleHandlerApplication
.
class
);
}
}
\ No newline at end of file
src/main/java/com/yagudza/controller/RoleController.java
0 → 100644
View file @
a9b217a7
package
com
.
yagudza
.
controller
;
import
com.yagudza.domain.Role
;
import
com.yagudza.dto.UserDto
;
import
com.yagudza.servise.RoleService
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.RestController
;
@RestController
public
class
RoleController
{
RoleService
roleService
;
public
RoleController
(
RoleService
roleService
)
{
this
.
roleService
=
roleService
;
}
@GetMapping
(
"/showRoles"
)
public
ResponseEntity
<
Iterable
<
Role
>>
showUsers
()
{
return
ResponseEntity
.
ok
(
roleService
.
findAll
());
}
}
src/main/java/com/yagudza/controller/UserController.java
View file @
a9b217a7
package
com
.
yagudza
.
controller
;
import
com.yagudza.dto.UserResponseDto
;
import
com.yagudza.dto.UserDto
;
import
com.yagudza.dto.UsersDto
;
import
com.yagudza.servise.UserService
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
...
...
@@ -18,7 +18,7 @@ public class UserController {
}
@GetMapping
(
"/showUsers"
)
public
ResponseEntity
<
Iterable
<
User
s
Dto
>>
showUsers
()
{
public
ResponseEntity
<
Iterable
<
UserDto
>>
showUsers
()
{
return
ResponseEntity
.
ok
(
userService
.
findAll
());
}
...
...
@@ -28,8 +28,13 @@ public class UserController {
}
@DeleteMapping
(
"/delete/{login}"
)
@ResponseStatus
(
HttpStatus
.
OK
)
@ResponseStatus
(
HttpStatus
.
NO_CONTENT
)
public
void
deleteUserByLogin
(
@PathVariable
(
"login"
)
String
login
)
{
userService
.
deleteById
(
login
);
}
@PostMapping
(
"/addUser"
)
public
ResponseEntity
<
UserResponseDto
>
addUser
(
@RequestBody
UserDto
userDto
)
{
return
ResponseEntity
.
ok
(
userService
.
createNewUser
(
userDto
));
}
}
src/main/java/com/yagudza/domain/Role.java
View file @
a9b217a7
...
...
@@ -9,17 +9,24 @@ public class Role {
@Id
@GeneratedValue
(
strategy
=
GenerationType
.
IDENTITY
)
l
ong
id
;
L
ong
id
;
String
name
;
@ManyToMany
(
mappedBy
=
"roles"
,
fetch
=
FetchType
.
LAZY
)
Set
<
User
>
userSet
=
new
HashSet
<>();
public
long
getId
()
{
// @ManyToMany(mappedBy = "roles")
// Set<User> users;
//
// public Set<User> getUsers() {
// return users;
// }
//
// public void setUsers(Set<User> users) {
// this.users = users;
// }
//
public
Long
getId
()
{
return
id
;
}
public
void
setId
(
l
ong
id
)
{
public
void
setId
(
L
ong
id
)
{
this
.
id
=
id
;
}
...
...
src/main/java/com/yagudza/domain/User.java
View file @
a9b217a7
package
com
.
yagudza
.
domain
;
import
com.fasterxml.jackson.annotation.JsonIgnore
;
import
javax.persistence.*
;
import
java.util.HashSet
;
import
java.util.Objects
;
import
java.util.Set
;
@Entity
public
class
User
{
@Column
(
nullable
=
false
)
...
...
@@ -18,17 +21,17 @@ public class User {
@Column
(
nullable
=
false
)
private
String
password
;
@ManyToMany
(
cascade
=
CascadeType
.
MERGE
,
fetch
=
FetchType
.
LAZY
)
@ManyToMany
(
cascade
=
{
CascadeType
.
PERSIST
,
CascadeType
.
DETACH
,
CascadeType
.
MERGE
}
,
fetch
=
FetchType
.
LAZY
)
@JoinTable
(
name
=
"user_role"
,
uniqueConstraints
=
{
@UniqueConstraint
(
columnNames
=
{
"user_login"
,
"role_id"
})},
joinColumns
=
{
@JoinColumn
(
name
=
"user_login"
,
referencedColumnName
=
"login"
)}
,
inverseJoinColumns
=
{
@JoinColumn
(
name
=
"role_id"
,
referencedColumnName
=
"id"
)
})
private
Set
<
Role
>
roles
;
// = new HashSet<>
();
private
Set
<
Role
>
roles
=
new
HashSet
();
public
Set
<
Role
>
getRoles
()
{
return
roles
;
return
roles
.
isEmpty
()
?
new
HashSet
<>()
:
roles
;
}
public
void
setRoles
(
Set
<
Role
>
roles
)
{
...
...
@@ -64,8 +67,7 @@ public class User {
return
"User{"
+
"name='"
+
name
+
'\''
+
", login='"
+
login
+
'\''
+
", password='"
+
password
+
'\''
+
", roles="
+
roles
+
", password='"
+
password
+
'}'
;
}
...
...
@@ -76,12 +78,11 @@ public class User {
User
user
=
(
User
)
o
;
return
Objects
.
equals
(
name
,
user
.
name
)
&&
Objects
.
equals
(
login
,
user
.
login
)
&&
Objects
.
equals
(
password
,
user
.
password
)
&&
Objects
.
equals
(
roles
,
user
.
roles
);
Objects
.
equals
(
password
,
user
.
password
);
}
@Override
public
int
hashCode
()
{
return
Objects
.
hash
(
name
,
login
,
password
,
roles
);
return
Objects
.
hash
(
name
,
login
,
password
);
}
}
src/main/java/com/yagudza/dto/RoleDto.java
0 → 100644
View file @
a9b217a7
package
com
.
yagudza
.
dto
;
public
class
RoleDto
{
Long
id
;
String
name
;
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
;
}
}
src/main/java/com/yagudza/dto/UserDto.java
View file @
a9b217a7
package
com
.
yagudza
.
dto
;
import
com.fasterxml.jackson.annotation.JsonInclude
;
import
com.yagudza.domain.Role
;
import
javax.persistence.*
;
import
java.util.HashSet
;
import
java.util.Set
;
public
class
UserDto
{
...
...
@@ -10,6 +12,7 @@ public class UserDto {
private
String
name
;
private
String
login
;
private
String
password
;
@JsonInclude
(
JsonInclude
.
Include
.
NON_EMPTY
)
private
Set
<
Role
>
roles
;
public
String
getName
()
{
...
...
@@ -37,7 +40,7 @@ public class UserDto {
}
public
Set
<
Role
>
getRoles
()
{
return
roles
;
return
roles
==
null
?
new
HashSet
<>()
:
roles
;
}
public
void
setRoles
(
Set
<
Role
>
roles
)
{
...
...
src/main/java/com/yagudza/dto/UserResponseDto.java
0 → 100644
View file @
a9b217a7
package
com
.
yagudza
.
dto
;
import
com.fasterxml.jackson.annotation.JsonInclude
;
import
org.springframework.stereotype.Service
;
import
java.util.ArrayList
;
import
java.util.List
;
@Service
public
class
UserResponseDto
{
private
Boolean
success
;
@JsonInclude
(
JsonInclude
.
Include
.
NON_EMPTY
)
private
List
<
String
>
errors
=
new
ArrayList
<>();
public
Boolean
getSuccess
()
{
return
success
;
}
public
void
setSuccess
(
Boolean
success
)
{
this
.
success
=
success
;
}
public
List
<
String
>
getErrors
()
{
return
errors
;
}
public
void
setErrors
(
List
<
String
>
errors
)
{
this
.
errors
=
errors
;
}
public
void
setError
(
String
error
)
{
this
.
errors
.
add
(
error
);
}
}
src/main/java/com/yagudza/mappers/UserMapper.java
0 → 100644
View file @
a9b217a7
package
com
.
yagudza
.
mappers
;
import
com.yagudza.domain.User
;
import
com.yagudza.dto.UserDto
;
import
org.mapstruct.Mapper
;
import
org.mapstruct.Mapping
;
import
org.mapstruct.factory.Mappers
;
@Mapper
public
interface
UserMapper
{
UserMapper
INSTANCE
=
Mappers
.
getMapper
(
UserMapper
.
class
);
@Mapping
(
target
=
"roles"
,
ignore
=
true
)
UserDto
toDto
(
User
user
);
Iterable
<
UserDto
>
toDto
(
Iterable
<
User
>
users
);
User
toUser
(
UserDto
userDto
);
}
src/main/java/com/yagudza/repository/UserRepository.java
View file @
a9b217a7
package
com
.
yagudza
.
repository
;
import
com.yagudza.domain.User
;
import
org.springframework.data.jpa.repository.EntityGraph
;
import
org.springframework.data.jpa.repository.Modifying
;
import
org.springframework.data.jpa.repository.Query
;
import
org.springframework.data.repository.CrudRepository
;
...
...
@@ -12,7 +14,7 @@ public interface UserRepository extends CrudRepository<User, String> {
@Query
(
nativeQuery
=
true
,
value
=
"select u.login, u.name, u.password from User u "
)
Iterable
<
User
>
findAll
();
@Query
(
nativeQuery
=
true
,
value
=
"select u.login, u.name, u.password from User u where u.login =
?1
"
)
@Query
(
nativeQuery
=
true
,
value
=
"select u.login, u.name, u.password from User u where u.login =
:var
"
)
Optional
<
User
>
findById
(
String
var
);
}
src/main/java/com/yagudza/servise/RoleService.java
View file @
a9b217a7
package
com
.
yagudza
.
servise
;
import
com.yagudza.domain.Role
;
import
com.yagudza.dto.RoleDto
;
import
com.yagudza.dto.UserDto
;
import
org.springframework.stereotype.Service
;
@Service
public
interface
RoleService
{
Iterable
<
Role
>
findAll
();
}
src/main/java/com/yagudza/servise/RoleServiceImpl.java
View file @
a9b217a7
package
com
.
yagudza
.
servise
;
public
class
RoleServiceImpl
{
import
com.yagudza.domain.Role
;
import
com.yagudza.repository.RoleRepository
;
import
org.springframework.stereotype.Service
;
@Service
public
class
RoleServiceImpl
implements
RoleService
{
private
final
RoleRepository
roleRepository
;
public
RoleServiceImpl
(
RoleRepository
roleRepository
)
{
this
.
roleRepository
=
roleRepository
;
}
@Override
public
Iterable
<
Role
>
findAll
(){
return
roleRepository
.
findAll
();
}
}
src/main/java/com/yagudza/servise/UserService.java
View file @
a9b217a7
package
com
.
yagudza
.
servise
;
import
com.yagudza.dto.UserResponseDto
;
import
com.yagudza.dto.UserDto
;
import
com.yagudza.dto.UsersDto
;
import
org.springframework.stereotype.Service
;
import
java.util.LinkedHashMap
;
import
java.util.LinkedList
;
@Service
public
interface
UserService
{
Iterable
<
User
s
Dto
>
findAll
();
Iterable
<
UserDto
>
findAll
();
UserDto
findById
(
String
login
);
void
deleteById
(
String
login
);
UserResponseDto
createNewUser
(
UserDto
userDto
);
UserResponseDto
updateNewUser
(
UserDto
userDto
);
}
src/main/java/com/yagudza/servise/UserServiceImpl.java
View file @
a9b217a7
package
com
.
yagudza
.
servise
;
import
com.yagudza.domain.User
;
import
com.yagudza.dto.UserResponseDto
;
import
com.yagudza.dto.UserDto
;
import
com.yagudza.dto.UsersDto
;
import
com.yagudza.exceptions.UserNotFoundException
;
import
com.yagudza.mappers.UserMapper
;
import
com.yagudza.repository.UserRepository
;
import
org.modelmapper.ModelMapper
;
import
org.springframework.stereotype.Service
;
import
java.util.stream.Collectors
;
import
java.util.stream.StreamSupport
;
@Service
public
class
UserServiceImpl
implements
UserService
{
private
final
UserRepository
userRepository
;
private
final
ModelMapper
modelMapper
;
public
UserServiceImpl
(
UserRepository
userRepository
,
ModelMapper
modelMapper
)
{
public
UserServiceImpl
(
UserRepository
userRepository
)
{
this
.
userRepository
=
userRepository
;
this
.
modelMapper
=
modelMapper
;
}
@Override
public
Iterable
<
UsersDto
>
findAll
()
{
Iterable
<
User
>
usersIterable
=
userRepository
.
findAll
();
return
StreamSupport
.
stream
(
usersIterable
.
spliterator
(),
false
)
.
map
(
user
->
modelMapper
.
map
(
user
,
UsersDto
.
class
))
.
collect
(
Collectors
.
toList
());
public
Iterable
<
UserDto
>
findAll
()
{
Iterable
<
User
>
users
=
userRepository
.
findAll
();
return
UserMapper
.
INSTANCE
.
toDto
(
users
);
}
@Override
public
UserDto
findById
(
String
login
)
{
Object
user
=
userRepository
.
findById
(
login
).
orElseThrow
(
UserNotFoundException:
:
new
);
return
modelMapper
.
map
(
user
,
UserDto
.
class
);
User
user
=
userRepository
.
findById
(
login
).
orElseThrow
(
UserNotFoundException:
:
new
);
return
UserMapper
.
INSTANCE
.
toDto
(
user
);
}
@Override
public
void
deleteById
(
String
login
)
{
if
(!
userRepository
.
existsById
(
login
))
{
throw
new
UserNotFoundException
()
;
return
;
}
userRepository
.
deleteById
(
login
);
}
public
UserResponseDto
createNewUser
(
UserDto
userDto
)
{
UserResponseDto
response
=
new
UserResponseDto
();
if
(
userRepository
.
existsById
(
userDto
.
getLogin
()))
{
response
.
setError
(
"User has been already created"
);
}
else
if
(
userDto
.
getLogin
()
==
null
||
userDto
.
getLogin
().
trim
().
equals
(
""
))
{
response
.
setError
(
"Login is empty"
);
}
if
(
userDto
.
getName
()
==
null
||
userDto
.
getName
().
trim
().
equals
(
""
))
{
response
.
setError
(
"Name is empty"
);
}
if
(
userDto
.
getLogin
()
==
null
||
userDto
.
getLogin
().
trim
().
equals
(
""
))
{
response
.
setError
(
"Login is empty"
);
}
else
{
if
(!
userDto
.
getPassword
().
matches
(
"((?=.*[1-9])(?=.*[A-Z])).+"
))
{
response
.
setError
(
"Password must have symbol in upper case and digit"
);
}
if
(!
userDto
.
getPassword
().
matches
(
".{6,20}"
))
response
.
setError
(
"Password must contain more then 6 and less then 20 symbols"
);
}
if
(
userDto
.
getRoles
()
==
null
||
userDto
.
getRoles
().
isEmpty
())
{
response
.
setError
(
"User must have one or more roles"
);
}
if
(!
response
.
getErrors
().
isEmpty
())
{
response
.
setSuccess
(
false
);
}
else
{
response
.
setSuccess
(
true
);
User
user
=
UserMapper
.
INSTANCE
.
toUser
(
userDto
);
userRepository
.
save
(
user
);
}
return
response
;
}
public
UserResponseDto
updateNewUser
(
UserDto
userDto
)
{
return
new
UserResponseDto
();
// stub
}
private
UserResponseDto
check
(
UserDto
userDto
,
UserResponseDto
response
)
{
return
new
UserResponseDto
();
// stub
}
}
src/main/resources/application.properties
View file @
a9b217a7
server.port
=
7777
spring.datasource.url
=
jdbc:h2:mem:testdb
spring.datasource.driverClassName
=
org.h2.Driver
spring.datasource.username
=
sa
...
...
@@ -11,6 +13,6 @@ spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
#spring.datasource.password=root
spring.jpa.show-sql
=
true
spring.jpa.hibernate.ddl-auto
=
upd
ate
spring.jpa.hibernate.ddl-auto
=
cre
ate
spring.h2.console.enabled
=
true
\ No newline at end of file
#
spring.h2.console.enabled
=
true
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment