Skip to content

Commit

Permalink
Merge pull request #8 from januschung/single-profile
Browse files Browse the repository at this point in the history
update profile and add tests
  • Loading branch information
januschung authored Jun 21, 2024
2 parents 2a07274 + d715c35 commit c43c8ac
Show file tree
Hide file tree
Showing 6 changed files with 270 additions and 61 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,6 @@ buildNumber.properties
.settings/

mvnw
mvnw.cmd
mvnw.cmd

.idea
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ To keep track of job applications:

1. Use Google Spreadsheet (I am using it before this project). It is ugly, hard to maintain and easy to mess up the layout.
1. Use online tool like huntr.co. But it can only track 40 jobs and I don't want to spend a dime for the premium service.
1. Use pen and paper. It works but I can't copy and paste information such as link job link and utilize it later.
1. Use pen and paper. It works but I can't copy and paste information such as the job url link and utilize it later.

None of the about is fun or totally fit my use case.

Expand All @@ -19,7 +19,7 @@ That's the reason for me to build something for myself, and potentially you, to

With Job Winner, you can keep track of how many job applications you want, for free, without paying for a fee and have your personal data being sold.

Another feature of Job Winner is the profile page. With that one can store useful information a job application usually asks for (eg. LinkedIn url) in a single section. Besides that, the profile page provides a handy feature to automatically copy the field that you click on to your clipboard.
Another feature of Job Winner is the `Profile Page`. With that one can store useful information a job application usually asks for (eg. LinkedIn url) in a single section. Besides that, the `Profile Page` provides a handy feature to automatically copy the field that you click on to your clipboard.

## Features

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ public class ProfileController {

@MutationMapping
public Mono<Profile> addProfile(@Argument AddProfileInput addProfileInput) {
Mono<Profile> profile = profileService.addProfile(addProfileInput);
return profile;
return profileService.addProfile(addProfileInput);
}

@MutationMapping
Expand All @@ -32,8 +31,12 @@ public Mono<Profile> updateProfile(@Argument Profile profile) {

@QueryMapping
public Flux<Profile> allProfile() {
Flux<Profile> profile = profileService.allProfile();
return profile;
return profileService.allProfile();
}

@QueryMapping
public Mono<Profile> getProfile(@Argument Integer id) {
return profileService.getProfile(id);
}

}
105 changes: 51 additions & 54 deletions src/main/java/com/tnite/jobwinner/service/ProfileService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,79 +7,76 @@
import com.tnite.jobwinner.model.Profile;
import com.tnite.jobwinner.repo.ProfileRepository;

import graphql.com.google.common.base.Function;
import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@Service
@Slf4j
public class ProfileService {

@Autowired
private ProfileRepository profileRepository;

Function<AddProfileInput, Profile> mapping = p -> {
var profile = new Profile();
profile.setFirstName(p.getFirstName());
profile.setLastName(p.getLastName());
profile.setAddressStreet1(p.getAddressStreet1());
profile.setAddressStreet2(p.getAddressStreet2());
profile.setAddressCity(p.getAddressCity());
profile.setAddressState(p.getAddressState());
profile.setAddressZip(p.getAddressZip());
profile.setLinkedin(p.getLinkedin());
profile.setGithub(p.getGithub());
profile.setPersonalWebsite(p.getPersonalWebsite());
return profile;
};

Function<Profile, Profile> editMapping = p -> {

private Profile mapToProfile(AddProfileInput addProfileInput) {
var profile = new Profile();
profile.setId(p.getId());
profile.setFirstName(p.getFirstName());
profile.setLastName(p.getLastName());
profile.setAddressStreet1(p.getAddressStreet1());
profile.setAddressStreet2(p.getAddressStreet2());
profile.setAddressCity(p.getAddressCity());
profile.setAddressState(p.getAddressState());
profile.setAddressZip(p.getAddressZip());
profile.setLinkedin(p.getLinkedin());
profile.setGithub(p.getGithub());
profile.setPersonalWebsite(p.getPersonalWebsite());
profile.setFirstName(addProfileInput.getFirstName());
profile.setLastName(addProfileInput.getLastName());
profile.setAddressStreet1(addProfileInput.getAddressStreet1());
profile.setAddressStreet2(addProfileInput.getAddressStreet2());
profile.setAddressCity(addProfileInput.getAddressCity());
profile.setAddressState(addProfileInput.getAddressState());
profile.setAddressZip(addProfileInput.getAddressZip());
profile.setLinkedin(addProfileInput.getLinkedin());
profile.setGithub(addProfileInput.getGithub());
profile.setPersonalWebsite(addProfileInput.getPersonalWebsite());
return profile;
};


}

public Mono<Profile> addProfile(AddProfileInput addProfileInput) {
Mono<Profile> profile = profileRepository.save(mapping.apply(addProfileInput));
log.info("Added new profile: {}", addProfileInput);
return profile;
Profile profile = mapToProfile(addProfileInput);
return profileRepository.save(profile)
.doOnSuccess(p -> log.info("Added new profile: {}", p))
.doOnError(e -> log.error("Failed to add profile: {}", addProfileInput, e));
}


public Mono<Profile> updateProfile(Profile profile) {
log.info("Updating profile id {}, {}", profile.getId());
return this.profileRepository.findById(profile.getId())
.flatMap(p -> {
p.setFirstName(profile.getFirstName());
p.setLastName(profile.getLastName());
p.setAddressStreet1(profile.getAddressStreet1());
p.setAddressStreet2(profile.getAddressStreet2());
p.setAddressCity(profile.getAddressCity());
p.setAddressState(profile.getAddressState());
p.setAddressZip(profile.getAddressZip());
p.setLinkedin(profile.getLinkedin());
p.setGithub(profile.getGithub());
p.setPersonalWebsite(profile.getPersonalWebsite());
return profileRepository.save(profile).log();
});
return profileRepository.findById(profile.getId())
.flatMap(existingProfile -> {
updateProfileDetails(existingProfile, profile);
return profileRepository.save(existingProfile);
})
.doOnSuccess(p -> log.info("Updated profile: {}", p))
.doOnError(e -> log.error("Failed to update profile: {}", profile, e));
}

private void updateProfileDetails(Profile existingProfile, Profile updatedProfile) {
existingProfile.setFirstName(updatedProfile.getFirstName());
existingProfile.setLastName(updatedProfile.getLastName());
existingProfile.setAddressStreet1(updatedProfile.getAddressStreet1());
existingProfile.setAddressStreet2(updatedProfile.getAddressStreet2());
existingProfile.setAddressCity(updatedProfile.getAddressCity());
existingProfile.setAddressState(updatedProfile.getAddressState());
existingProfile.setAddressZip(updatedProfile.getAddressZip());
existingProfile.setLinkedin(updatedProfile.getLinkedin());
existingProfile.setGithub(updatedProfile.getGithub());
existingProfile.setPersonalWebsite(updatedProfile.getPersonalWebsite());
}


public Flux<Profile> allProfile() {
return this.profileRepository.findAll().log();
return profileRepository.findAll()
.doOnComplete(() -> log.info("Retrieved all profiles"))
.doOnError(e -> log.error("Failed to retrieve profiles", e));
}

public Mono<Profile> getProfile(Integer id) {
return profileRepository.findById(id)
.switchIfEmpty(Mono.defer(() -> {
log.warn("Profile with id {} not found", id);
return Mono.empty();
}))
.doOnSuccess(profile -> log.info("Retrieved profile: {}", profile))
.doOnError(e -> log.error("Failed to retrieve profile with id {}", id, e));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
Expand Down Expand Up @@ -66,4 +67,14 @@ void testAllProfileWhenNoProfilesThenReturnEmptyResult() {

assertEquals(0, result.collectList().block().size());
}

@Test
void getProfile() {
Profile profile1 = new Profile();
when(profileService.getProfile(anyInt())).thenReturn(Mono.just(profile1));

Mono<Profile> result = profileController.getProfile(1);

assertEquals(profile1, result.block());
}
}
Loading

0 comments on commit c43c8ac

Please sign in to comment.