Skip to content

Commit 36e4686

Browse files
committed
implement register api handler
1 parent 4640d21 commit 36e4686

File tree

14 files changed

+525
-0
lines changed

14 files changed

+525
-0
lines changed

pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@
4242
<groupId>mysql</groupId>
4343
<artifactId>mysql-connector-java</artifactId>
4444
</dependency>
45+
<dependency>
46+
<groupId>org.apache.commons</groupId>
47+
<artifactId>commons-lang3</artifactId>
48+
</dependency>
4549

4650
<dependency>
4751
<groupId>org.springframework.boot</groupId>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.taskagile.domain.application;
2+
3+
import com.taskagile.domain.application.commands.RegistrationCommand;
4+
import com.taskagile.domain.model.user.RegistrationException;
5+
6+
public interface UserService {
7+
8+
/**
9+
* Register a new user with username, email address, and password.
10+
*
11+
* @param command instance of <code>RegistrationCommand</code>
12+
* @throws RegistrationException when registration failed. Possible reasons are:
13+
* 1) Username already exists
14+
* 2) Email address already exists.
15+
*/
16+
void register(RegistrationCommand command) throws RegistrationException;
17+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.taskagile.domain.application.commands;
2+
3+
public class RegistrationCommand {
4+
5+
private String username;
6+
private String emailAddress;
7+
private String password;
8+
9+
public RegistrationCommand(String username, String emailAddress, String password) {
10+
this.username = username;
11+
this.emailAddress = emailAddress;
12+
this.password = password;
13+
}
14+
15+
public String getUsername() {
16+
return this.username;
17+
}
18+
19+
public String getEmailAddress() {
20+
return this.emailAddress;
21+
}
22+
23+
public String getPassword() {
24+
return this.password;
25+
}
26+
27+
@Override
28+
public boolean equals(Object o) {
29+
if (this == o) return true;
30+
if (o == null || getClass() != o.getClass()) return false;
31+
RegistrationCommand that = (RegistrationCommand) o;
32+
if (username != null ? !username.equals(that.username) : that.username != null) return false;
33+
if (emailAddress != null ? !emailAddress.equals(that.emailAddress) : that.emailAddress != null) return false;
34+
return password != null ? password.equals(that.password) : that.password == null;
35+
}
36+
37+
@Override
38+
public int hashCode() {
39+
int result = username != null ? username.hashCode() : 0;
40+
result = 31 * result + (emailAddress != null ? emailAddress.hashCode() : 0);
41+
result = 31 * result + (password != null ? password.hashCode() : 0);
42+
return result;
43+
}
44+
45+
@Override
46+
public String toString() {
47+
return "RegistrationCommand{" +
48+
"username='" + username + '\'' +
49+
", emailAddress='" + emailAddress + '\'' +
50+
", password='" + password + '\'' +
51+
'}';
52+
}
53+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.taskagile.domain.application.impl;
2+
3+
import com.taskagile.domain.application.UserService;
4+
import com.taskagile.domain.application.commands.RegistrationCommand;
5+
import com.taskagile.domain.model.user.RegistrationException;
6+
import org.springframework.stereotype.Service;
7+
8+
@Service
9+
public class UserServiceImpl implements UserService {
10+
11+
@Override
12+
public void register(RegistrationCommand command) throws RegistrationException {
13+
// TODO implement this
14+
}
15+
16+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.taskagile.domain.model.user;
2+
3+
public class EmailAddressExistsException extends RegistrationException {
4+
5+
private static final long serialVersionUID = -7856406258381199164L;
6+
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.taskagile.domain.model.user;
2+
3+
public class RegistrationException extends Exception {
4+
5+
private static final long serialVersionUID = -2737904013752279210L;
6+
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.taskagile.domain.model.user;
2+
3+
public class UsernameExistsException extends RegistrationException {
4+
5+
private static final long serialVersionUID = -7856406258381199164L;
6+
7+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.taskagile.utils;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
public final class JsonUtils {
9+
10+
private static final Logger log = LoggerFactory.getLogger(JsonUtils.class);
11+
12+
private JsonUtils() {
13+
}
14+
15+
public static String toJson(Object object) {
16+
ObjectMapper mapper = new ObjectMapper();
17+
try {
18+
return mapper.writeValueAsString(object);
19+
} catch (JsonProcessingException e) {
20+
log.error("Failed to convert object to JSON string", e);
21+
return null;
22+
}
23+
}
24+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.taskagile.web.apis;
2+
3+
import com.taskagile.domain.application.UserService;
4+
import com.taskagile.domain.model.user.EmailAddressExistsException;
5+
import com.taskagile.domain.model.user.RegistrationException;
6+
import com.taskagile.domain.model.user.UsernameExistsException;
7+
import com.taskagile.web.payload.RegistrationPayload;
8+
import com.taskagile.web.results.ApiResult;
9+
import com.taskagile.web.results.Result;
10+
import org.springframework.http.ResponseEntity;
11+
import org.springframework.stereotype.Controller;
12+
import org.springframework.web.bind.annotation.PostMapping;
13+
import org.springframework.web.bind.annotation.RequestBody;
14+
15+
import javax.validation.Valid;
16+
17+
@Controller
18+
public class RegistrationApiController {
19+
20+
private UserService service;
21+
22+
public RegistrationApiController(UserService service) {
23+
this.service = service;
24+
}
25+
26+
@PostMapping("/api/registrations")
27+
public ResponseEntity<ApiResult> register(@Valid @RequestBody RegistrationPayload payload) {
28+
try {
29+
service.register(payload.toCommand());
30+
return Result.created();
31+
} catch (RegistrationException e) {
32+
String errorMessage = "Registration failed";
33+
if (e instanceof UsernameExistsException) {
34+
errorMessage = "Username already exists";
35+
} else if (e instanceof EmailAddressExistsException) {
36+
errorMessage = "Email address already exists";
37+
}
38+
return Result.failure(errorMessage);
39+
}
40+
}
41+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.taskagile.web.payload;
2+
3+
import com.taskagile.domain.application.commands.RegistrationCommand;
4+
5+
import javax.validation.constraints.Email;
6+
import javax.validation.constraints.NotNull;
7+
import javax.validation.constraints.Size;
8+
9+
public class RegistrationPayload {
10+
11+
@Size(min = 2, max = 50, message = "Username must be between 2 and 50 characters")
12+
@NotNull
13+
private String username;
14+
15+
@Email(message = "Email address should be valid")
16+
@Size(max = 100, message = "Email address must not be more than 100 characters")
17+
@NotNull
18+
private String emailAddress;
19+
20+
@Size(min = 6, max = 30, message = "Password must be between 6 and 30 characters")
21+
@NotNull
22+
private String password;
23+
24+
public RegistrationCommand toCommand() {
25+
return new RegistrationCommand(this.username, this.emailAddress, this.password);
26+
}
27+
28+
public String getUsername() {
29+
return username;
30+
}
31+
32+
public void setUsername(String username) {
33+
this.username = username;
34+
}
35+
36+
public String getEmailAddress() {
37+
return emailAddress;
38+
}
39+
40+
public void setEmailAddress(String emailAddress) {
41+
this.emailAddress = emailAddress;
42+
}
43+
44+
public String getPassword() {
45+
return password;
46+
}
47+
48+
public void setPassword(String password) {
49+
this.password = password;
50+
}
51+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy