-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
25c5c46
commit c223aad
Showing
13 changed files
with
212 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
services/users/src/main/java/com/workup/users/commands/ClientRegisterCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.workup.users.commands; | ||
|
||
import static com.workup.users.commands.utils.PasswordHasher.hashPassword; | ||
|
||
import com.workup.shared.commands.users.requests.ClientRegisterRequest; | ||
import com.workup.shared.commands.users.responses.SignUpAndInResponse; | ||
import com.workup.shared.enums.HttpStatusCode; | ||
import com.workup.shared.enums.users.UserType; | ||
import com.workup.users.db.Client; | ||
import java.util.Objects; | ||
|
||
public class ClientRegisterCommand extends UserCommand<ClientRegisterRequest, SignUpAndInResponse> { | ||
|
||
@Override | ||
public SignUpAndInResponse Run(ClientRegisterRequest request) { | ||
if (Objects.isNull(request.getEmail()) | ||
|| Objects.isNull(request.getPassword()) | ||
|| Objects.isNull(request.getClientName())) { | ||
return SignUpAndInResponse.builder() | ||
.withStatusCode(HttpStatusCode.BAD_REQUEST) | ||
.withSuccess(false) | ||
.build(); | ||
} | ||
try { | ||
Client client = | ||
Client.builder() | ||
.withEmail(request.getEmail()) | ||
.withPassword_hash(hashPassword(request.getPassword())) | ||
.withClient_name(request.getClientName()) | ||
.withIndustry(request.getIndustry()) | ||
.withClient_description(request.getDescription()) | ||
.withEmployee_count(request.getEmployeeCount()) | ||
.withCity(request.getCity()) | ||
.build(); | ||
Client savedClient = clientRepository.save(client); | ||
|
||
return SignUpAndInResponse.builder() | ||
.withSuccess(true) | ||
.withUserName(savedClient.getEmail()) | ||
.withUserId(savedClient.getId().toString()) | ||
.withUserType(UserType.FREELANCER) | ||
.withStatusCode(HttpStatusCode.OK) | ||
.build(); | ||
} catch (Exception e) { | ||
return SignUpAndInResponse.builder() | ||
.withStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR) | ||
.withSuccess(false) | ||
.build(); | ||
} | ||
} | ||
} |
36 changes: 0 additions & 36 deletions
36
services/users/src/main/java/com/workup/users/commands/FreelancerRegister.java
This file was deleted.
Oops, something went wrong.
59 changes: 59 additions & 0 deletions
59
services/users/src/main/java/com/workup/users/commands/FreelancerRegisterCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.workup.users.commands; | ||
|
||
import static com.workup.users.commands.utils.PasswordHasher.hashPassword; | ||
|
||
import com.workup.shared.commands.payments.wallet.requests.CreateWalletRequest; | ||
import com.workup.shared.commands.users.requests.FreelancerRegisterRequest; | ||
import com.workup.shared.commands.users.responses.SignUpAndInResponse; | ||
import com.workup.shared.enums.HttpStatusCode; | ||
import com.workup.shared.enums.ServiceQueueNames; | ||
import com.workup.shared.enums.users.UserType; | ||
import com.workup.users.db.Freelancer; | ||
import java.util.Objects; | ||
|
||
public class FreelancerRegisterCommand | ||
extends UserCommand<FreelancerRegisterRequest, SignUpAndInResponse> { | ||
|
||
@Override | ||
public SignUpAndInResponse Run(FreelancerRegisterRequest request) { | ||
if (Objects.isNull(request.getEmail()) | ||
|| Objects.isNull(request.getPassword()) | ||
|| Objects.isNull(request.getFullName())) { | ||
return SignUpAndInResponse.builder() | ||
.withStatusCode(HttpStatusCode.BAD_REQUEST) | ||
.withSuccess(false) | ||
.build(); | ||
} | ||
try { | ||
Freelancer freelancer = | ||
Freelancer.builder() | ||
.withEmail(request.getEmail()) | ||
.withPassword_hash(hashPassword(request.getPassword())) | ||
.withFull_name(request.getFullName()) | ||
.withJob_title(request.getJobTitle()) | ||
.withCity(request.getCity()) | ||
.build(); | ||
Freelancer savedFreelancer = freelancerRepository.save(freelancer); | ||
// create wallet | ||
CreateWalletRequest createWalletRequest = | ||
CreateWalletRequest.builder() | ||
.withUserId(savedFreelancer.getId().toString()) | ||
.withFreelancerId(savedFreelancer.getId().toString()) | ||
.build(); | ||
rabbitTemplate.convertSendAndReceive(ServiceQueueNames.PAYMENTS, createWalletRequest); | ||
|
||
return SignUpAndInResponse.builder() | ||
.withSuccess(true) | ||
.withUserName(savedFreelancer.getEmail()) | ||
.withUserId(savedFreelancer.getId().toString()) | ||
.withUserType(UserType.FREELANCER) | ||
.withStatusCode(HttpStatusCode.OK) | ||
.build(); | ||
} catch (Exception e) { | ||
return SignUpAndInResponse.builder() | ||
.withStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR) | ||
.withSuccess(false) | ||
.build(); | ||
} | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
services/users/src/main/java/com/workup/users/commands/LoginCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.workup.users.commands; | ||
|
||
import com.workup.shared.commands.users.requests.LoginRequest; | ||
import com.workup.shared.commands.users.responses.SignUpAndInResponse; | ||
import com.workup.shared.enums.HttpStatusCode; | ||
import com.workup.shared.enums.users.UserType; | ||
import com.workup.users.commands.utils.PasswordHasher; | ||
import com.workup.users.db.Client; | ||
import com.workup.users.db.Freelancer; | ||
import java.util.Optional; | ||
|
||
public class LoginCommand extends UserCommand<LoginRequest, SignUpAndInResponse> { | ||
|
||
@Override | ||
public SignUpAndInResponse Run(LoginRequest request) { | ||
String email = request.getEmail(); | ||
String password = request.getPassword(); | ||
try { | ||
Optional<Client> client = clientRepository.findByEmail(email); | ||
if (client.isPresent()) { | ||
if (PasswordHasher.checkPassword(password, client.get().getPassword_hash())) { | ||
return SignUpAndInResponse.builder() | ||
.withSuccess(true) | ||
.withUserName(client.get().getEmail()) | ||
.withUserId(client.get().getId().toString()) | ||
.withUserType(UserType.CLIENT) | ||
.withStatusCode(HttpStatusCode.OK) | ||
.build(); | ||
} | ||
} | ||
// check if freelancer | ||
Optional<Freelancer> freelancer = freelancerRepository.findByEmail(email); | ||
if (freelancer.isPresent()) { | ||
if (PasswordHasher.checkPassword(password, freelancer.get().getPassword_hash())) { | ||
return SignUpAndInResponse.builder() | ||
.withSuccess(true) | ||
.withUserName(freelancer.get().getEmail()) | ||
.withUserId(freelancer.get().getId().toString()) | ||
.withUserType(UserType.FREELANCER) | ||
.withStatusCode(HttpStatusCode.OK) | ||
.build(); | ||
} | ||
} | ||
|
||
// return unauthorized | ||
return SignUpAndInResponse.builder() | ||
.withSuccess(false) | ||
.withStatusCode(HttpStatusCode.UNAUTHORIZED) | ||
.build(); | ||
} catch (Exception e) { | ||
return SignUpAndInResponse.builder() | ||
.withStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR) | ||
.withSuccess(false) | ||
.build(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 5 additions & 1 deletion
6
services/users/src/main/java/com/workup/users/repositories/ClientRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
package com.workup.users.repositories; | ||
|
||
import com.workup.users.db.Client; | ||
import java.util.Optional; | ||
import org.springframework.data.mongodb.repository.MongoRepository; | ||
|
||
public interface ClientRepository extends MongoRepository<Client, String> {} | ||
public interface ClientRepository extends MongoRepository<Client, String> { | ||
// find by clientEmail | ||
Optional<Client> findByEmail(String email); | ||
} |
6 changes: 5 additions & 1 deletion
6
services/users/src/main/java/com/workup/users/repositories/FreelancerRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
package com.workup.users.repositories; | ||
|
||
import com.workup.users.db.Freelancer; | ||
import java.util.Optional; | ||
import org.springframework.data.mongodb.repository.MongoRepository; | ||
|
||
public interface FreelancerRepository extends MongoRepository<Freelancer, String> {} | ||
public interface FreelancerRepository extends MongoRepository<Freelancer, String> { | ||
// find by freelancerEmail | ||
Optional<Freelancer> findByEmail(String email); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters