Skip to content

Commit 8c72dce

Browse files
committed
create the api contract for endpoints and model for users, and make entity, service, controller, repository, and mapper, validator, and password encryption. Also implemented the test. Code is commented!
1 parent 27ca478 commit 8c72dce

File tree

19 files changed

+1086
-4
lines changed

19 files changed

+1086
-4
lines changed

api-contract/src/main/resources/api-contract.yaml

Lines changed: 189 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ tags:
1313
- name: menu
1414
- name: index
1515
- name: log
16+
- name: user
1617

1718
paths:
1819
/:
@@ -310,7 +311,7 @@ paths:
310311
get:
311312
tags:
312313
- log
313-
summary: Get all log
314+
summary: Get all logs
314315
operationId: renderAllLogs
315316
responses:
316317
'200':
@@ -355,6 +356,120 @@ paths:
355356
'405':
356357
description: Invalid input
357358

359+
/api/admin/user-service/users:
360+
get:
361+
tags:
362+
- user
363+
summary: Get all Users
364+
operationId: renderAllUsers
365+
responses:
366+
'200':
367+
description: Successful operation
368+
content:
369+
application/json:
370+
schema:
371+
type: array
372+
items:
373+
$ref: '#/components/schemas/UserModel'
374+
'400':
375+
description: Bad request
376+
'401':
377+
description: Unauthorized
378+
'403':
379+
description: Forbidden
380+
'404':
381+
description: User not found
382+
'405':
383+
description: Validation exception
384+
385+
post:
386+
tags:
387+
- user
388+
summary: Add new user in a service
389+
description: Add new user in a service and save to database
390+
operationId: addNewUser
391+
requestBody:
392+
description: Create a user and store it
393+
content:
394+
application/json:
395+
schema:
396+
$ref: '#/components/schemas/UserNewModel'
397+
required: true
398+
responses:
399+
'201':
400+
description: Successful operation
401+
content:
402+
application/json:
403+
schema:
404+
$ref: '#/components/schemas/UserModel'
405+
'405':
406+
description: Invalid input
407+
408+
/api/admin/user-service/users/{userId}:
409+
get:
410+
tags:
411+
- user
412+
summary: Get user by Id
413+
operationId: renderUserById
414+
parameters:
415+
- name: userId
416+
in: path
417+
description: ID of the user to update
418+
required: true
419+
schema:
420+
type: string
421+
format: uuid
422+
example: '3a8ea9f1-1a95-4caf-932f-2f988052933b'
423+
responses:
424+
'200':
425+
description: Successful operation
426+
content:
427+
application/json:
428+
schema:
429+
$ref: '#/components/schemas/UserModel'
430+
'400':
431+
description: Bad request
432+
'401':
433+
description: Unauthorized
434+
'403':
435+
description: Forbidden
436+
'404':
437+
description: User not found
438+
'405':
439+
description: Validation exception
440+
441+
put:
442+
tags:
443+
- user
444+
summary: Modify an existing user in a service
445+
description: Modify an existing user in a service and save to database
446+
operationId: modifyUserById
447+
parameters:
448+
- name: userId
449+
in: path
450+
description: ID of the user to update
451+
required: true
452+
schema:
453+
type: string
454+
format: uuid
455+
example: '3a8ea9f1-1a95-4caf-932f-2f988052933b'
456+
requestBody:
457+
description: Create a user and store it
458+
content:
459+
application/json:
460+
schema:
461+
$ref: '#/components/schemas/UserModel'
462+
required: true
463+
responses:
464+
'201':
465+
description: Successful operation
466+
content:
467+
application/json:
468+
schema:
469+
$ref: '#/components/schemas/UserModifyModel'
470+
'405':
471+
description: Invalid input
472+
358473
components:
359474
schemas:
360475
RepresentativeAdminModel:
@@ -589,4 +704,76 @@ components:
589704
level:
590705
type: string
591706
serviceName:
592-
type: string
707+
type: string
708+
709+
UserNewModel:
710+
type: object
711+
properties:
712+
username:
713+
type: string
714+
password:
715+
type: string
716+
repeatPassword:
717+
type: string
718+
email:
719+
type: string
720+
required:
721+
- username
722+
- password
723+
- repeatPassword
724+
- email
725+
726+
UserModel:
727+
type: object
728+
properties:
729+
id:
730+
description: Id of the User
731+
type: string
732+
format: uuid
733+
example: '3a8ea9f1-1a95-4caf-932f-2f988052933b'
734+
createdAt:
735+
description: Creation time of User entity
736+
type: string
737+
format: datetime
738+
example: '2019-01-21T05:47:08.644'
739+
updatedAt:
740+
description: Updated time of User entity
741+
type: string
742+
format: datetime
743+
example: '2019-01-21T05:47:08.644'
744+
createdBy:
745+
description: Admin id who created the User entity
746+
type: string
747+
format: uuid
748+
example: '3a8ea9f1-1a95-4caf-932f-2f988052933b'
749+
updatedBy:
750+
description: User/Admin id who updated the User entity and with setting availability to DELETE, will achieve a soft delete
751+
type: string
752+
format: uuid
753+
example: '3a8ea9f1-1a95-4caf-932f-2f988052933b'
754+
username:
755+
type: string
756+
email:
757+
type: string
758+
role:
759+
$ref: '#/components/schemas/Role'
760+
availability:
761+
$ref: '#/components/schemas/Availability'
762+
UserModifyModel:
763+
type: object
764+
properties:
765+
username:
766+
type: string
767+
email:
768+
type: string
769+
role:
770+
$ref: '#/components/schemas/Role'
771+
availability:
772+
$ref: '#/components/schemas/Availability'
773+
774+
Role:
775+
type: string
776+
enum:
777+
- ADMIN
778+
- USER
779+
- SUPER_ADMIN

api-gateway/src/main/resources/application.properties

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,7 @@ spring.cloud.gateway.routes[3].id=logging-service
3434
spring.cloud.gateway.routes[3].uri=lb://logging-service
3535
spring.cloud.gateway.routes[3].predicates[0]=Path=/api/admin/logging-service/**
3636

37-
37+
# User Service Route:
38+
spring.cloud.gateway.routes[4].id=user-service
39+
spring.cloud.gateway.routes[4].uri=lb://user-service
40+
spring.cloud.gateway.routes[4].predicates[0]=Path=/api/admin/user-service/**

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
<maven.compiler.target>17</maven.compiler.target>
3131
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
3232
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
33-
<project.version>3.0.0-SNAPSHOT</project.version>
33+
<project.version>3.2.0-SNAPSHOT</project.version>
3434
<spring-cloud.version>2022.0.2</spring-cloud.version>
3535
</properties>
3636

user-service/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,18 @@
7979
<artifactId>h2</artifactId>
8080
<scope>test</scope>
8181
</dependency>
82+
<dependency>
83+
<groupId>com.csaba79coder</groupId>
84+
<artifactId>api-contract</artifactId>
85+
<version>3.0.2</version>
86+
<scope>compile</scope>
87+
</dependency>
88+
<dependency>
89+
<groupId>org.glassfish.hk2.external</groupId>
90+
<artifactId>aopalliance-repackaged</artifactId>
91+
<version>3.0.3</version>
92+
<scope>test</scope>
93+
</dependency>
8294
</dependencies>
8395

8496
</project>

user-service/src/main/java/com/csaba79coder/userservice/UserServiceApplication.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,35 @@
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
55
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
66

7+
/**
8+
* This is the main class for the User Service application. It is
9+
* annotated with @SpringBootApplication, which is a convenience
10+
* annotation that combines three other annotations:
11+
* - @Configuration: indicates that this class provides configuration
12+
* information for the application context.
13+
* - @EnableAutoConfiguration: enables Spring Boot's autoconfiguration
14+
* feature, which automatically configures the application based on
15+
* classpath settings, other beans, and various property settings.
16+
* - @ComponentScan: tells Spring to scan this package and its
17+
* sub-packages for components (i.e., beans) that can be autowired
18+
* into other beans.
19+
*
20+
* Additionally, this class is annotated with @EnableDiscoveryClient,
21+
* which enables it to register with a service registry (such as Eureka)
22+
* as a discovery client. This allows other services to discover and
23+
* communicate with this service.
24+
*/
725
@SpringBootApplication
826
@EnableDiscoveryClient // added @EnableDiscoveryClient for this to work as a discovery client
927
public class UserServiceApplication {
1028

29+
/**
30+
* This is the main method for the User Service application. It
31+
* starts the Spring application context and launches the
32+
* application.
33+
*
34+
* @param args command-line arguments passed to the application
35+
*/
1136
public static void main(String[] args) {
1237
SpringApplication.run(UserServiceApplication.class, args);
1338
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.csaba79coder.userservice.controller;
2+
3+
import com.csaba79coder.bestprotocol.api.UserApi;
4+
import com.csaba79coder.bestprotocol.model.UserModel;
5+
import com.csaba79coder.bestprotocol.model.UserModifyModel;
6+
import com.csaba79coder.bestprotocol.model.UserNewModel;
7+
import com.csaba79coder.userservice.model.service.UserService;
8+
import lombok.RequiredArgsConstructor;
9+
import org.springframework.http.ResponseEntity;
10+
import org.springframework.web.bind.annotation.RestController;
11+
12+
import java.util.List;
13+
import java.util.UUID;
14+
15+
/**
16+
* responsible for the endpoints
17+
*/
18+
@RestController
19+
@RequiredArgsConstructor
20+
public class UserController implements UserApi {
21+
22+
private final UserService userService;
23+
24+
/**
25+
* responsible for the endpoints
26+
* @return the user model
27+
* @param body the user model
28+
*/
29+
@Override
30+
public ResponseEntity<UserModel> addNewUser(UserNewModel body) {
31+
return ResponseEntity.status(201).body(userService.addNewUser(body));
32+
}
33+
34+
/**
35+
* responsible for the endpoints
36+
* @param userId the user id
37+
* @return the user modify model
38+
*/
39+
@Override
40+
public ResponseEntity<UserModifyModel> modifyUserById(UUID userId, UserModel body) {
41+
return ResponseEntity.status(200).body(userService.modifyUserById(userId, body));
42+
}
43+
44+
/**
45+
* responsible for the endpoints
46+
* @return the user model
47+
*/
48+
@Override
49+
public ResponseEntity<List<UserModel>> renderAllUsers() {
50+
return ResponseEntity.status(200).body(userService.renderAllUsers());
51+
}
52+
53+
/**
54+
* responsible for the endpoints
55+
* @param userId
56+
* @return the user model
57+
*/
58+
@Override
59+
public ResponseEntity<UserModel> renderUserById(UUID userId) {
60+
return ResponseEntity.status(200).body(userService.renderUserById(userId));
61+
}
62+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.csaba79coder.userservice.model.base.entity;
2+
3+
import jakarta.persistence.Column;
4+
import jakarta.persistence.MappedSuperclass;
5+
import lombok.Getter;
6+
import lombok.Setter;
7+
import org.hibernate.annotations.CreationTimestamp;
8+
9+
import java.time.LocalDateTime;
10+
import java.util.UUID;
11+
12+
/**
13+
* A base class for entities that need to be audited. This class provides the common audit fields such as created_at,
14+
* updated_at, created_by and updated_by.
15+
*
16+
* This class is annotated with @MappedSuperclass which means that the properties of this class will be mapped to the
17+
* properties of its subclasses.
18+
*/
19+
@MappedSuperclass
20+
@Getter
21+
@Setter
22+
public class Auditable extends Identifier {
23+
24+
@CreationTimestamp
25+
@Column(name = "created_at", nullable = false)
26+
private LocalDateTime createdAt = LocalDateTime.now();
27+
28+
@CreationTimestamp
29+
@Column(name = "updated_at", nullable = false)
30+
private LocalDateTime updatedAt = LocalDateTime.now();
31+
32+
@Column(name = "created_by")
33+
private UUID createdBy = UUID.fromString("6772c9dc-a7be-4826-963a-e376074fd4e7");
34+
35+
@Column(name = "updated_by")
36+
private UUID updatedBy = UUID.fromString("dbd58012-9ee7-47d5-8f87-9bbc91583009");
37+
}

0 commit comments

Comments
 (0)