-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from januschung/offer
offer and test
- Loading branch information
Showing
20 changed files
with
524 additions
and
63 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
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
40 changes: 40 additions & 0 deletions
40
src/main/java/com/tnite/jobwinner/controller/OfferController.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,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); | ||
} | ||
|
||
} |
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
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,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); | ||
} |
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
67 changes: 67 additions & 0 deletions
67
src/main/java/com/tnite/jobwinner/service/OfferService.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,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)); | ||
} | ||
} |
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
Oops, something went wrong.