Skip to content

Commit

Permalink
added client set photo commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Pandemic1617 committed May 8, 2024
1 parent 1f34452 commit 43da4fd
Show file tree
Hide file tree
Showing 16 changed files with 150 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.workup.users.commands;

import com.workup.shared.commands.users.requests.ClientGetPhotoRequest;
import com.workup.shared.commands.users.responses.ClientGetPhotoResponse;
import com.workup.shared.enums.HttpStatusCode;
import com.workup.users.db.Client;
import java.util.Optional;

public class ClientGetPhotoCommand
extends UserCommand<ClientGetPhotoRequest, ClientGetPhotoResponse> {

@Override
public ClientGetPhotoResponse Run(ClientGetPhotoRequest request) {
Optional<Client> clientOptional = clientRepository.findById(request.user_id);

if (!clientOptional.isPresent()) {
return ClientGetPhotoResponse.builder()
.withStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR)
.build();
}

Client client = clientOptional.get();

return ClientGetPhotoResponse.builder()
.withStatusCode(HttpStatusCode.OK)
.withPhotoLink(client.getPhoto_link())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.workup.users.commands;

import com.workup.shared.commands.users.requests.ClientSetPhotoRequest;
import com.workup.shared.commands.users.responses.ClientSetPhotoResponse;
import com.workup.shared.enums.HttpStatusCode;
import com.workup.users.db.Client;
import java.util.Optional;

public class ClientSetPhotoCommand
extends UserCommand<ClientSetPhotoRequest, ClientSetPhotoResponse> {

@Override
public ClientSetPhotoResponse Run(ClientSetPhotoRequest request) {

Optional<Client> clientOption = clientRepository.findById(request.user_id);

if (!clientOption.isPresent()) {
throw new RuntimeException("User not found");
}

Client client = clientOption.get();

client.setPhoto_link(request.photoLink);

clientRepository.save(client);

return ClientSetPhotoResponse.builder().withStatusCode(HttpStatusCode.OK).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,27 @@
import com.workup.shared.commands.users.requests.FreelancerGetPhotoRequest;
import com.workup.shared.commands.users.responses.FreelancerGetPhotoResponse;
import com.workup.shared.enums.HttpStatusCode;
import com.workup.users.db.Client;
import java.util.Base64;
import com.workup.users.db.Freelancer;
import java.util.Optional;

public class FreelancerGetPhotoCommand
extends UserCommand<FreelancerGetPhotoRequest, FreelancerGetPhotoResponse> {

@Override
public FreelancerGetPhotoResponse Run(FreelancerGetPhotoRequest request) {
Optional<Client> clientOptional = clientRepository.findById(request.user_id);
Optional<Freelancer> freelancerOptional = freelancerRepository.findById(request.user_id);

if (!clientOptional.isPresent()) {
if (!freelancerOptional.isPresent()) {
return FreelancerGetPhotoResponse.builder()
.withStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR)
.build();
}
String name = PHOTO_BUCKET + request.user_id;

byte[] bytesArr;
try {
bytesArr = gridFsTemplate.getResource(name).getInputStream().readAllBytes();
} catch (Exception e) {
return FreelancerGetPhotoResponse.builder()
.withStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR)
.build();
}

String base64Encoded = Base64.getEncoder().encodeToString(bytesArr);
Freelancer freelancer = freelancerOptional.get();

return FreelancerGetPhotoResponse.builder()
.withStatusCode(HttpStatusCode.OK)
.withPhotoEncoded(base64Encoded)
.withPhotoLink(freelancer.getPhoto_link())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,27 @@
import com.workup.shared.commands.users.requests.FreelancerGetResumeRequest;
import com.workup.shared.commands.users.responses.FreelancerGetResumeResponse;
import com.workup.shared.enums.HttpStatusCode;
import com.workup.users.db.Client;
import java.util.Base64;
import com.workup.users.db.Freelancer;
import java.util.Optional;

public class FreelancerGetResumeCommand
extends UserCommand<FreelancerGetResumeRequest, FreelancerGetResumeResponse> {

@Override
public FreelancerGetResumeResponse Run(FreelancerGetResumeRequest request) {
Optional<Client> clientOptional = clientRepository.findById(request.user_id);
Optional<Freelancer> freelancerOptional = freelancerRepository.findById(request.user_id);

if (!clientOptional.isPresent()) {
if (!freelancerOptional.isPresent()) {
return FreelancerGetResumeResponse.builder()
.withStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR)
.build();
}
String name = RESUME_BUCKET + request.user_id;

byte[] bytesArr;
try {
bytesArr = gridFsTemplate.getResource(name).getInputStream().readAllBytes();
} catch (Exception e) {
return FreelancerGetResumeResponse.builder()
.withStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR)
.build();
}

String base64Encoded = Base64.getEncoder().encodeToString(bytesArr);
Freelancer freelancer = freelancerOptional.get();

return FreelancerGetResumeResponse.builder()
.withStatusCode(HttpStatusCode.OK)
.withResumeEncoded(base64Encoded)
.withResumeLink(freelancer.getResume_link())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,27 @@
import com.workup.shared.commands.users.requests.FreelancerSetPhotoRequest;
import com.workup.shared.commands.users.responses.FreelancerSetPhotoResponse;
import com.workup.shared.enums.HttpStatusCode;
import java.io.ByteArrayInputStream;
import java.util.Base64;
import com.workup.users.db.Freelancer;
import java.util.Optional;

public class FreelancerSetPhotoCommand
extends UserCommand<FreelancerSetPhotoRequest, FreelancerSetPhotoResponse> {

@Override
public FreelancerSetPhotoResponse Run(FreelancerSetPhotoRequest request) {

String name = PHOTO_BUCKET + request.user_id;
Optional<Freelancer> freelancerOption = freelancerRepository.findById(request.user_id);

byte[] photo_bytes_arr = Base64.getDecoder().decode(request.photo_encoded);

try {
gridFsTemplate.store(new ByteArrayInputStream(photo_bytes_arr), name);
} catch (Exception e) {
return FreelancerSetPhotoResponse.builder()
.withStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR)
.build();
if (!freelancerOption.isPresent()) {
throw new RuntimeException("User not found");
}

Freelancer freelancer = freelancerOption.get();

freelancer.setPhoto_link(request.photoLink);

freelancerRepository.save(freelancer);

return FreelancerSetPhotoResponse.builder().withStatusCode(HttpStatusCode.OK).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,27 @@
import com.workup.shared.commands.users.requests.FreelancerSetResumeRequest;
import com.workup.shared.commands.users.responses.FreelancerSetResumeResponse;
import com.workup.shared.enums.HttpStatusCode;
import java.io.ByteArrayInputStream;
import java.util.Base64;
import com.workup.users.db.Freelancer;
import java.util.Optional;

public class FreelancerSetResumeCommand
extends UserCommand<FreelancerSetResumeRequest, FreelancerSetResumeResponse> {

@Override
public FreelancerSetResumeResponse Run(FreelancerSetResumeRequest request) {

String name = RESUME_BUCKET + request.user_id;
Optional<Freelancer> freelancerOption = freelancerRepository.findById(request.user_id);

byte[] resume_byes_arr = Base64.getDecoder().decode(request.resume_encoded);

try {
gridFsTemplate.store(new ByteArrayInputStream(resume_byes_arr), name);
} catch (Exception e) {
return FreelancerSetResumeResponse.builder()
.withStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR)
.build();
if (!freelancerOption.isPresent()) {
throw new RuntimeException("User not found");
}

Freelancer freelancer = freelancerOption.get();

freelancer.setResume_link(request.resumeLink);

freelancerRepository.save(freelancer);

return FreelancerSetResumeResponse.builder().withStatusCode(HttpStatusCode.OK).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class Client {
private String client_name;
private String industry;
private String city;
private String photo_id;
private String photo_link;
private String client_description;
private Integer employee_count;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ public class Freelancer {
private Date created_at;
private String full_name;
private Date birthdate;
private String resume_id;
private String resume_link;
private String city;
private String job_title;
private String photo_id;
private String photo_link;
private String description;
private List<String> skills;
private List<String> languages;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.workup.shared.commands.users.requests;

import com.workup.shared.commands.CommandRequest;
import lombok.Getter;
import lombok.experimental.SuperBuilder;
import lombok.extern.jackson.Jacksonized;

@Getter
@SuperBuilder(setterPrefix = "with")
@Jacksonized
public class ClientGetPhotoRequest extends CommandRequest {
public String user_id;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.workup.shared.commands.users.requests;

import com.workup.shared.commands.CommandRequest;
import lombok.Getter;
import lombok.experimental.SuperBuilder;
import lombok.extern.jackson.Jacksonized;

@Getter
@SuperBuilder(setterPrefix = "with")
@Jacksonized
public class ClientSetPhotoRequest extends CommandRequest {
public String user_id;
public String photoLink;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
@Jacksonized
public class FreelancerSetPhotoRequest extends CommandRequest {
public String user_id;
public String photo_encoded; // Base64 encoded photo
public String photoLink;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
@Jacksonized
public class FreelancerSetResumeRequest extends CommandRequest {
public String user_id;
public String resume_encoded; // Base64 encoded photo
public String resumeLink;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.workup.shared.commands.users.responses;

import com.workup.shared.commands.CommandResponse;
import lombok.Getter;
import lombok.experimental.SuperBuilder;
import lombok.extern.jackson.Jacksonized;

@Getter
@SuperBuilder(setterPrefix = "with")
@Jacksonized
public class ClientGetPhotoResponse extends CommandResponse {
public String photoLink;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.workup.shared.commands.users.responses;

import com.workup.shared.commands.CommandResponse;
import lombok.Getter;
import lombok.experimental.SuperBuilder;
import lombok.extern.jackson.Jacksonized;

@Getter
@SuperBuilder(setterPrefix = "with")
@Jacksonized
public class ClientSetPhotoResponse extends CommandResponse {}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
@SuperBuilder(setterPrefix = "with")
@Jacksonized
public class FreelancerGetPhotoResponse extends CommandResponse {
public String photoEncoded; // Base64 encoded photo
public String photoLink;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
@SuperBuilder(setterPrefix = "with")
@Jacksonized
public class FreelancerGetResumeResponse extends CommandResponse {
public String resumeEncoded; // Base64 encoded resume
public String resumeLink;
}

0 comments on commit 43da4fd

Please sign in to comment.