Skip to content

Commit

Permalink
Added Login and Register Commands
Browse files Browse the repository at this point in the history
  • Loading branch information
ShimaaBetah committed May 9, 2024
1 parent 25c5c46 commit c223aad
Show file tree
Hide file tree
Showing 13 changed files with 212 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,19 @@ public RemoveFreelancerLanguageResponse receive(RemoveFreelancerLanguageRequest
return ((RemoveFreelancerLanguageCommand) commandMap.getCommand("RemoveFreelancerLanguage"))
.Run(in);
}

@RabbitHandler
public SignUpAndInResponse receive(LoginRequest in) throws Exception {
return ((LoginCommand) commandMap.getCommand("Login")).Run(in);
}

@RabbitHandler
public SignUpAndInResponse receive(FreelancerRegisterRequest in) throws Exception {
return ((FreelancerRegisterCommand) commandMap.getCommand("FreelancerRegister")).Run(in);
}

@RabbitHandler
public SignUpAndInResponse receive(ClientRegisterRequest in) throws Exception {
return ((ClientRegisterCommand) commandMap.getCommand("ClientRegister")).Run(in);
}
}
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();
}
}
}

This file was deleted.

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();
}
}
}
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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.workup.users.repositories.ExperienceRepository;
import com.workup.users.repositories.FreelancerRepository;
import lombok.Setter;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;

Expand All @@ -28,5 +29,6 @@ public abstract class UserCommand<

@Setter ClientRepository clientRepository;

@Setter AmqpTemplate rabbitTemplate;
@Autowired GridFsTemplate gridFsTemplate;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.workup.shared.commands.CommandRequest;
import com.workup.shared.commands.CommandResponse;
import com.workup.users.repositories.*;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

Expand All @@ -15,6 +16,7 @@ public class UserCommandMap
@Autowired ClientRepository clientRepository;
@Autowired EducationRepository educationRepository;
@Autowired AchievementRepository achievementRepository;
@Autowired AmqpTemplate rabbitTemplate;

public void registerCommands() {

Expand Down Expand Up @@ -56,6 +58,10 @@ public void registerCommands() {
commands.put("UpdateFreelancerAchievement", UpdateFreelancerAchievementCommand.class);
commands.put("UpdateFreelancerEducation", UpdateFreelancerEducationCommand.class);
commands.put("UpdateFreelancerExperience", UpdateFreelancerExperienceCommand.class);

commands.put("Login", LoginCommand.class);
commands.put("FreelancerRegister", FreelancerRegisterCommand.class);
commands.put("ClientRegister", ClientRegisterCommand.class);
// NEW_COMMAND_BOILERPLATE
}

Expand All @@ -67,5 +73,6 @@ public void setupCommand(
command.setClientRepository(clientRepository);
command.setEducationRepository(educationRepository);
command.setAchievementRepository(achievementRepository);
command.setRabbitTemplate(rabbitTemplate);
}
}
4 changes: 3 additions & 1 deletion services/users/src/main/java/com/workup/users/db/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
@Document
public class Client {
@Id private ObjectId id;
@Indexed private String email;

@Indexed(unique = true)
private String email;

private String password_hash;
private Date created_at;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
@Document(collection = "Freelancer")
public class Freelancer {
@Id private ObjectId id;
@Indexed private String email;

@Indexed(unique = true)
private String email;

private String password_hash;
private Date created_at;
Expand Down
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);
}
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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public class ClientRegisterRequest extends CommandRequest {
private String clientName;
private String industry;
private String city;
private String photoId;
private String description;
private Integer employeeCount;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.workup.shared.commands.users.responses;

import com.workup.shared.commands.CommandResponse;
import com.workup.shared.enums.users.UserType;
import lombok.Getter;
import lombok.experimental.SuperBuilder;
import lombok.extern.jackson.Jacksonized;
Expand All @@ -9,5 +10,8 @@
@SuperBuilder(setterPrefix = "with")
@Jacksonized
public class SignUpAndInResponse extends CommandResponse {
String authToken;
boolean success;
String userName;
String userId;
UserType userType;
}

0 comments on commit c223aad

Please sign in to comment.