Skip to content

Commit

Permalink
Merge pull request #23 from januschung/offer
Browse files Browse the repository at this point in the history
offer and test
  • Loading branch information
januschung authored Dec 10, 2024
2 parents 89c593b + c404bf2 commit 527b2a0
Show file tree
Hide file tree
Showing 20 changed files with 524 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.tnite.jobwinner.controller;

import com.tnite.jobwinner.model.Interview;
import com.tnite.jobwinner.model.AddInterviewInput;
import com.tnite.jobwinner.model.InterviewInput;
import com.tnite.jobwinner.service.InterviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.graphql.data.method.annotation.Argument;
Expand All @@ -20,7 +20,7 @@ public class InterviewController {
private InterviewService interviewService;

@MutationMapping
public Mono<Interview> addInterview(@Argument AddInterviewInput interviewInput) {
public Mono<Interview> addInterview(@Argument InterviewInput interviewInput) {
return interviewService.addInterview(interviewInput);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.springframework.web.reactive.config.CorsRegistry;
import org.springframework.web.reactive.config.WebFluxConfigurer;

import com.tnite.jobwinner.model.AddJobApplicationInput;
import com.tnite.jobwinner.model.JobApplicationInput;
import com.tnite.jobwinner.model.JobApplication;
import com.tnite.jobwinner.service.JobApplicationService;

Expand Down Expand Up @@ -39,7 +39,7 @@ public void addCorsMappings(CorsRegistry registry) {


@MutationMapping
public Mono<JobApplication> addJobApplication(@Argument AddJobApplicationInput addJobApplicationInput) {
public Mono<JobApplication> addJobApplication(@Argument JobApplicationInput addJobApplicationInput) {
Mono<JobApplication> jobApplication = jobApplicationService.addJobApplication(addJobApplicationInput);
return jobApplication;
}
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/com/tnite/jobwinner/controller/OfferController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.tnite.jobwinner.controller;

import com.tnite.jobwinner.model.OfferInput;
import com.tnite.jobwinner.model.Offer;
import com.tnite.jobwinner.service.OfferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.graphql.data.method.annotation.Argument;
import org.springframework.graphql.data.method.annotation.MutationMapping;
import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.stereotype.Controller;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@Controller
public class OfferController {

@Autowired
private OfferService offerService;

@MutationMapping
public Mono<Offer> addOffer(@Argument OfferInput offerInput) {
return offerService.addOffer(offerInput);
}

@MutationMapping
public Mono<Offer> updateOffer(@Argument Offer offer) {
return offerService.updateOffer(offer);
}

@QueryMapping
public Flux<Offer> allOffer() {
return offerService.allOffer();
}

@QueryMapping
public Mono<Offer> offerByJobApplicationId(@Argument Integer jobApplicationId) {
return offerService.offerByJobApplicationId(jobApplicationId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@Data
@NoArgsConstructor
@AllArgsConstructor
public class AddInterviewInput {
public class InterviewInput {
private Integer jobApplicationId;
private LocalDate interviewDate;
private String interviewer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@Data
@NoArgsConstructor
@AllArgsConstructor
public class AddJobApplicationInput {
public class JobApplicationInput {
private String companyName;
private String jobTitle;
private String salaryRange;
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/com/tnite/jobwinner/model/Offer.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.tnite.jobwinner.model;

import java.time.LocalDate;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Table;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
Expand All @@ -23,6 +25,5 @@ public class Offer {
@Column("salary_offered")
private String salaryOffered;
private String description;
private String status;

}
1 change: 0 additions & 1 deletion src/main/java/com/tnite/jobwinner/model/OfferInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@ public class OfferInput {
private LocalDate offerDate;
private String salaryOffered;
private String description;
private String status;

}
9 changes: 9 additions & 0 deletions src/main/java/com/tnite/jobwinner/repo/OfferRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.tnite.jobwinner.repo;

import com.tnite.jobwinner.model.Offer;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import reactor.core.publisher.Mono;

public interface OfferRepository extends ReactiveCrudRepository<Offer, Integer> {
Mono<Offer> findByJobApplicationId(Integer jobApplicationId);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.tnite.jobwinner.service;

import com.tnite.jobwinner.model.Interview;
import com.tnite.jobwinner.model.AddInterviewInput;
import com.tnite.jobwinner.model.InterviewInput;
import com.tnite.jobwinner.repo.InterviewRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -16,7 +16,7 @@ public class InterviewService {
@Autowired
private InterviewRepository interviewRepository;

private Interview mapToInterview(AddInterviewInput interviewInput) {
private Interview mapToInterview(InterviewInput interviewInput) {
var interview = new Interview();
interview.setJobApplicationId(interviewInput.getJobApplicationId());
interview.setInterviewDate(interviewInput.getInterviewDate());
Expand All @@ -26,7 +26,7 @@ private Interview mapToInterview(AddInterviewInput interviewInput) {
return interview;
}

public Mono<Interview> addInterview(AddInterviewInput interviewInput) {
public Mono<Interview> addInterview(InterviewInput interviewInput) {
Interview interview = mapToInterview(interviewInput);
return interviewRepository.save(interview)
.doOnSuccess(p -> log.info("Added new interview: {}", p))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tnite.jobwinner.model.AddJobApplicationInput;
import com.tnite.jobwinner.model.JobApplicationInput;
import com.tnite.jobwinner.model.JobApplication;
import com.tnite.jobwinner.repo.JobApplicationRepository;

Expand All @@ -22,7 +22,7 @@ public class JobApplicationService {
@Autowired
private JobApplicationRepository jobApplicationRepository;

Function<AddJobApplicationInput, JobApplication> mapping = aji -> {
Function<JobApplicationInput, JobApplication> mapping = aji -> {
var jobApplication = new JobApplication();
jobApplication.setCompanyName(aji.getCompanyName());
jobApplication.setJobTitle(aji.getJobTitle());
Expand All @@ -48,7 +48,7 @@ public class JobApplicationService {
};


public Mono<JobApplication> addJobApplication(AddJobApplicationInput addJobApplicationInput) {
public Mono<JobApplication> addJobApplication(JobApplicationInput addJobApplicationInput) {
Mono<JobApplication> jobApplication = jobApplicationRepository.save(mapping.apply(addJobApplicationInput));
log.info("Added new job application: {}", addJobApplicationInput);
return jobApplication;
Expand Down
67 changes: 67 additions & 0 deletions src/main/java/com/tnite/jobwinner/service/OfferService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.tnite.jobwinner.service;

import com.tnite.jobwinner.model.OfferInput;
import com.tnite.jobwinner.model.Offer;
import com.tnite.jobwinner.repo.OfferRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@Service
@Slf4j
public class OfferService {

@Autowired
private OfferRepository offerRepository;

private Offer mapToOffer(OfferInput offerInput) {
var offer = new Offer();
offer.setJobApplicationId(offerInput.getJobApplicationId());
offer.setOfferDate(offerInput.getOfferDate());
offer.setSalaryOffered(offerInput.getSalaryOffered());
offer.setDescription(offerInput.getDescription());
return offer;
}

public Mono<Offer> addOffer(OfferInput offerInput) {
Offer offer = mapToOffer(offerInput);
return offerRepository.save(offer)
.doOnSuccess(p -> log.info("Added new Offer: {}", p))
.doOnError(e -> log.error("Failed to add Offer: {}", offerInput, e));
}

public Mono<Offer> updateOffer(Offer offer) {
return offerRepository.findById(offer.getId())
.flatMap(existingOffer -> {
updateOfferDetails(existingOffer, offer);
return offerRepository.save(existingOffer);
})
.doOnSuccess(p -> log.info("Updated Offer: {}", p))
.doOnError(e -> log.error("Failed to update Offer: {}", offer, e));
}

private void updateOfferDetails(Offer existingOffer, Offer updatedOffer) {
existingOffer.setJobApplicationId(updatedOffer.getJobApplicationId());
existingOffer.setOfferDate(updatedOffer.getOfferDate());
existingOffer.setSalaryOffered(updatedOffer.getSalaryOffered());
existingOffer.setDescription(updatedOffer.getDescription());
}

public Flux<Offer> allOffer() {
return offerRepository.findAll()
.doOnComplete(() -> log.info("Retrieved all Offers"))
.doOnError(e -> log.error("Failed to retrieve Offers", e));
}

public Mono<Offer> offerByJobApplicationId(Integer jobApplicationId) {
return offerRepository.findByJobApplicationId(jobApplicationId)
.switchIfEmpty(Mono.defer(() ->{
log.warn("Offer with jobApplicationId {} not found", jobApplicationId);
return Mono.empty();
}))
.doOnSuccess(offer -> log.info("Retrieved Offer: {}", offer))
.doOnError(e -> log.error("Failed to retrieve Offer with jobApplicationId {}", jobApplicationId, e));
}
}
16 changes: 7 additions & 9 deletions src/main/resources/graphql/schema.graphqls
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
type Query {
jobApplicationById(id: ID): JobApplication
jobApplicationByCompanyName(companyName: String): [JobApplication]
allJobApplication: [JobApplication]
profileById(id: ID): Profile
searchJobApplications(searchTerm: String!): [JobApplication]
allInterview: [Interview]
allInterviewByJobApplicationId(jobApplicationId: Int!): [Interview]
interviewById(id: ID): Interview
allOfferByApplicationId(jobApplicationId: Int!): [Offer]
allOffer: [Offer]
offerByJobApplicationId(jobApplicationId: Int!): Offer
}

type JobApplication {
Expand Down Expand Up @@ -41,7 +41,6 @@ type Offer {
offerDate: String!
salaryOffered: String
description: String
status: String
}

type Interview {
Expand All @@ -60,8 +59,8 @@ type Mutation {
updateProfile(profile: UpdateProfileInput): Profile
addInterview(interviewInput: AddInterviewInput): Interview
updateInterview(id: ID!, interview: UpdateInterviewInput): Interview
addOffer(offerInput: AddOfferInput): Offer
updateOffer(id: ID!, offer: UpdateOfferInput): Offer
addOffer(offerInput: OfferInput): Offer
updateOffer(offer: UpdateOfferInput): Offer
}

input AddJobApplicationInput {
Expand Down Expand Up @@ -116,18 +115,17 @@ input UpdateInterviewInput {
status: String
}

input AddOfferInput {
input OfferInput {
jobApplicationId: Int!
offerDate: String!
salaryOffered: String
description: String
status: String
}

input UpdateOfferInput {
id: ID!
jobApplicationId: Int!
offerDate: String!
salaryOffered: String
description: String
status: String
}
}
4 changes: 2 additions & 2 deletions src/main/resources/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ create table if not exists offer (
offer_date date not null,
salary_offered varchar(255),
description varchar(500),
status varchar(20),
foreign key (job_application_id) references job_application(id) on delete cascade
foreign key (job_application_id) references job_application(id) on delete cascade,
constraint job_application_unique unique (job_application_id)
);

insert into profile(id)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.tnite.jobwinner.controller;

import com.tnite.jobwinner.model.AddInterviewInput;
import com.tnite.jobwinner.model.InterviewInput;
import com.tnite.jobwinner.model.Interview;
import com.tnite.jobwinner.service.InterviewService;
import org.junit.jupiter.api.Test;
Expand All @@ -27,9 +27,9 @@ class InterviewControllerTest {

@Test
void testAddInterview() {
AddInterviewInput addInterviewInput = new AddInterviewInput();
InterviewInput addInterviewInput = new InterviewInput();
Interview interview = new Interview();
when(interviewService.addInterview(any(AddInterviewInput.class))).thenReturn(Mono.just(interview));
when(interviewService.addInterview(any(InterviewInput.class))).thenReturn(Mono.just(interview));

Mono<Interview> result = interviewController.addInterview(addInterviewInput);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.tnite.jobwinner.controller;

import com.tnite.jobwinner.model.AddInterviewInput;
import com.tnite.jobwinner.model.AddJobApplicationInput;
import com.tnite.jobwinner.model.Interview;
import com.tnite.jobwinner.model.JobApplicationInput;
import com.tnite.jobwinner.model.JobApplication;
import com.tnite.jobwinner.service.JobApplicationService;
import org.junit.jupiter.api.Test;
Expand All @@ -28,9 +26,9 @@ class JobApplicationControllerTest {

@Test
void testAddJobApplication() {
AddJobApplicationInput addJobApplicationInput = new AddJobApplicationInput();
JobApplicationInput addJobApplicationInput = new JobApplicationInput();
JobApplication jobApplication = new JobApplication();
when(jobApplicationService.addJobApplication(any(AddJobApplicationInput.class))).thenReturn(Mono.just(jobApplication));
when(jobApplicationService.addJobApplication(any(JobApplicationInput.class))).thenReturn(Mono.just(jobApplication));

Mono<JobApplication> result = jobApplicationController.addJobApplication(addJobApplicationInput);

Expand Down
Loading

0 comments on commit 527b2a0

Please sign in to comment.