Skip to content

Commit a67984f

Browse files
Basic functionality. Register and Logging
1 parent e4d5700 commit a67984f

File tree

11 files changed

+225
-53
lines changed

11 files changed

+225
-53
lines changed

pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<version>${springdoc.version}</version>
4646
</dependency>
4747

48+
<!-- Utils-->
4849
<dependency>
4950
<groupId>org.projectlombok</groupId>
5051
<artifactId>lombok</artifactId>
@@ -59,6 +60,7 @@
5960
<scope>provided</scope>
6061
</dependency>
6162

63+
<!-- Database-->
6264
<dependency>
6365
<groupId>com.h2database</groupId>
6466
<artifactId>h2</artifactId>

src/main/java/com/springboot/msauthenticationservice/controller/UserApi.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import io.swagger.v3.oas.annotations.responses.ApiResponses;
88
import jakarta.validation.Valid;
99
import org.springframework.http.ResponseEntity;
10-
import org.springframework.web.bind.annotation.GetMapping;
1110
import org.springframework.web.bind.annotation.PostMapping;
1211
import org.springframework.web.bind.annotation.RequestBody;
1312
import org.springframework.web.bind.annotation.RequestMapping;
@@ -17,7 +16,7 @@ public interface UserApi {
1716

1817
/*
1918
* Sign up user
20-
* @Return no content
19+
* @Return user id
2120
*/
2221
@Operation(summary = "Sign Up User", description = "Signing up a user in the application", tags = {"User"})
2322
@ApiResponses(value ={
@@ -42,6 +41,6 @@ public interface UserApi {
4241
@ApiResponse(responseCode = "400", description = "Bad Request"),
4342
@ApiResponse(responseCode = "405", description = "Method Not Allowed")
4443
})
45-
@GetMapping(value = "/log_in", produces = {"application/json"})
46-
ResponseEntity<String> logInUserView(LogInDto logInDto);
44+
@PostMapping(value = "/log_in", produces = {"application/json"})
45+
ResponseEntity<LogInDto> logInUserView(@Valid @RequestBody LogInDto logInDto);
4746
}

src/main/java/com/springboot/msauthenticationservice/controller/impl/UserApiController.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
import com.springboot.msauthenticationservice.dto.SignUpDto;
66
import com.springboot.msauthenticationservice.service.UserService;
77
import lombok.extern.slf4j.Slf4j;
8-
import org.springframework.http.HttpStatus;
8+
import org.springframework.http.MediaType;
99
import org.springframework.http.ResponseEntity;
1010
import org.springframework.web.bind.annotation.RestController;
1111

12+
import java.net.URI;
13+
1214
/**
1315
* The Api User
1416
*/
@@ -26,25 +28,29 @@ public UserApiController(UserService userService){
2628
this.userService = userService;
2729
}
2830

31+
/**
32+
* Signing up a user
33+
* @param signUpDto the dto
34+
* @return the response entity
35+
*/
2936
@Override
3037
public ResponseEntity<String> signUpUserView(SignUpDto signUpDto) {
31-
try {
38+
var savedUser = this.userService.signUpUser(signUpDto);
39+
return ResponseEntity.created(URI.create(
40+
"/user/".concat(savedUser.getId().toString())))
41+
.contentType(MediaType.APPLICATION_JSON).body("Id: ".concat(savedUser.getId().toString()));
3242

33-
var value = this.userService.signUpUserView(signUpDto);
34-
return ResponseEntity.ok(value);
35-
}catch (Exception e){
36-
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
37-
}
3843
}
3944

45+
/**
46+
* Logging a user
47+
* @param logInDto the dto
48+
* @return the response entity
49+
*/
4050
@Override
41-
public ResponseEntity<String> logInUserView(LogInDto logInDto) {
42-
try {
43-
var value = this.userService.logInUserView(logInDto);
44-
51+
public ResponseEntity<LogInDto> logInUserView(LogInDto logInDto) {
52+
var value = this.userService.logInUser(logInDto);
4553
return ResponseEntity.ok(value);
46-
}catch (Exception e){
47-
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
48-
}
54+
4955
}
5056
}
Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,56 @@
11
package com.springboot.msauthenticationservice.dto;
22

3+
import com.fasterxml.jackson.annotation.JsonInclude;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
35
import jakarta.validation.constraints.Email;
46
import jakarta.validation.constraints.NotNull;
57
import lombok.*;
68

9+
import java.util.UUID;
710

8-
@Builder
9-
@Getter
10-
@Setter
11-
@ToString
11+
12+
@Data
1213
@AllArgsConstructor
1314
@NoArgsConstructor
1415
public class LogInDto {
1516
/**
17+
* Only sent in the api REQUEST
1618
* The email
1719
*/
20+
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
1821
@Email(message = "should be of type email")
1922
@NotNull
20-
public String email;
23+
private String email;
2124

2225
/**
26+
* Only sent in the api REQUEST
2327
* The password
2428
*/
2529
@NotNull
26-
public String password;
30+
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
31+
private String password;
32+
33+
/**
34+
* Only sent in the api RESPONSE
35+
* The username
36+
*/
37+
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
38+
@JsonInclude(JsonInclude.Include.NON_NULL)
39+
private String username;
40+
41+
/**
42+
* Only sent in the api RESPONSE
43+
* The id
44+
*/
45+
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
46+
@JsonInclude(JsonInclude.Include.NON_NULL)
47+
private UUID id;
48+
49+
public String getEmail(){
50+
return this.email;
51+
}
52+
53+
public String getPassword(){
54+
return this.password;
55+
}
2756
}

src/main/java/com/springboot/msauthenticationservice/dto/SignUpDto.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@
55
import jakarta.validation.constraints.NotNull;
66
import lombok.*;
77

8-
@Builder
9-
@Getter
10-
@Setter
11-
@ToString
8+
import java.util.UUID;
9+
10+
11+
@Data
1212
@AllArgsConstructor
1313
@NoArgsConstructor
1414
public class SignUpDto {
1515

16+
/**
17+
* The id
18+
*/
19+
private UUID id;
1620
/**
1721
* The username
1822
*/
@@ -30,4 +34,12 @@ public class SignUpDto {
3034
*/
3135
@NotNull
3236
private String password;
37+
38+
public UUID getId(){
39+
return this.id;
40+
}
41+
42+
public String getEmail() {
43+
return this.email;
44+
}
3345
}

src/main/java/com/springboot/msauthenticationservice/entity/User.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package com.springboot.msauthenticationservice.entity;
22

3-
import jakarta.persistence.Column;
4-
import jakarta.persistence.Entity;
5-
import jakarta.persistence.Id;
6-
import jakarta.persistence.Table;
3+
import jakarta.persistence.*;
74
import jakarta.validation.constraints.NotBlank;
85
import lombok.*;
6+
import org.hibernate.annotations.GenericGenerator;
97

108
import java.io.Serializable;
9+
import java.util.UUID;
1110

1211
@Builder
1312
@NoArgsConstructor
@@ -21,16 +20,17 @@ public class User implements Serializable {
2120
* The id
2221
*/
2322
@Id
24-
// @GeneratedValue(generator = "USERS_ID_SEQ")
25-
// @GenericGenerator(name = "USERS_ID_SEQ", strategy = "org.hibernate.id.UUIDGenerator")
23+
@GeneratedValue(generator = "USERS_ID_SEQ")
24+
@GenericGenerator(name = "USERS_ID_SEQ", strategy = "org.hibernate.id.UUIDGenerator")
2625
@Column(name = "id")
27-
private String id;
26+
private UUID id;
2827

2928
/**
3029
* The name
3130
*/
3231
@NotBlank(message = "Name is mandatory")
33-
private String name;
32+
@Column(name = "name")
33+
private String username;
3434

3535
/**
3636
* The email
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.springboot.msauthenticationservice.mapper;
2+
3+
import com.springboot.msauthenticationservice.dto.LogInDto;
4+
import com.springboot.msauthenticationservice.dto.SignUpDto;
5+
import com.springboot.msauthenticationservice.entity.User;
6+
7+
public interface UserMapper {
8+
9+
/**
10+
* Map SignUpDto
11+
* @param user the user
12+
* @return the SignUpDeto
13+
*/
14+
SignUpDto mapSignUp(User user);
15+
16+
/**
17+
* Map User
18+
* @param signUpDto the SignUpDto
19+
* @return the user
20+
*/
21+
User mapSignUp(SignUpDto signUpDto);
22+
23+
24+
/**
25+
* Map LogInDto
26+
* @param user the user
27+
* @return the LogInDto
28+
*/
29+
LogInDto mapLogIn(User user);
30+
31+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.springboot.msauthenticationservice.mapper.impl;
2+
3+
import com.springboot.msauthenticationservice.dto.LogInDto;
4+
import com.springboot.msauthenticationservice.dto.SignUpDto;
5+
import com.springboot.msauthenticationservice.entity.User;
6+
import com.springboot.msauthenticationservice.mapper.UserMapper;
7+
import org.springframework.beans.BeanUtils;
8+
import org.springframework.stereotype.Component;
9+
10+
@Component
11+
public class UserMapperImpl implements UserMapper {
12+
@Override
13+
public SignUpDto mapSignUp(User user) {
14+
if(user == null) {
15+
return null;
16+
}
17+
var signUpDto = new SignUpDto();
18+
BeanUtils.copyProperties(user,signUpDto);
19+
return signUpDto;
20+
}
21+
22+
@Override
23+
public User mapSignUp(SignUpDto signUpDto) {
24+
if(signUpDto == null) {
25+
return null;
26+
}
27+
28+
var user = new User();
29+
BeanUtils.copyProperties(signUpDto, user);
30+
return user;
31+
}
32+
33+
@Override
34+
public LogInDto mapLogIn(User user) {
35+
if(user == null) {
36+
return null;
37+
}
38+
var logInDto = new LogInDto();
39+
BeanUtils.copyProperties(user,logInDto);
40+
return logInDto;
41+
}
42+
43+
}

src/main/java/com/springboot/msauthenticationservice/repository/UserRepository.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,21 @@
99
/**
1010
* User Repository
1111
*/
12-
1312
@Repository
1413
public interface UserRepository extends JpaRepository<User, String> {
1514

1615
/**
1716
* Logging user
18-
* @param email
19-
* @param password
20-
* @return
17+
* @param email the email
18+
* @param password the password
19+
* @return the optional
20+
*/
21+
Optional<User> findByEmailIgnoreCaseAndPassword(String email, String password);
22+
23+
/**
24+
* Find a user by email
25+
* @param email the email
26+
* @return the optional
2127
*/
22-
Optional<User> findByEmailAndPassword(String email, String password);
28+
Optional<User> findByEmail(String email);
2329
}

src/main/java/com/springboot/msauthenticationservice/service/UserService.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
import com.springboot.msauthenticationservice.dto.LogInDto;
44
import com.springboot.msauthenticationservice.dto.SignUpDto;
55

6-
import java.io.IOException;
7-
86
public interface UserService {
9-
String signUpUserView (SignUpDto signUpDto) throws IOException;
7+
SignUpDto signUpUser (SignUpDto signUpDto);
108

11-
String logInUserView (LogInDto logInDto) throws IOException;
9+
LogInDto logInUser (LogInDto logInDto);
1210

1311
}

0 commit comments

Comments
 (0)