Wrote OfferWriter and following classes to save offers to persistence

This commit is contained in:
Leonie Eitze
2023-06-15 18:52:06 +02:00
parent 5436118829
commit 5204cc1710
7 changed files with 227 additions and 2 deletions

View File

@@ -0,0 +1,51 @@
package de.rwu.easydrop.api.dto;
import lombok.Data;
import de.rwu.easydrop.model.Product;
import java.util.Date;
/*
* Offer data transfer object
*/
@Data
public class OfferDTO {
/**
* Platform internal offer identifier.
*/
private String offerId;
/*
* The product that our software buys
*/
Product sourceProduct;
/*
* The product that our software sells
*/
Product saleProduct;
/*
* Date of the creation of the offer
* NOTE: Use Timestamp? https://docs.oracle.com/javase/8/docs/api/java/sql/Timestamp.html
*/
Date creationDate;
/*
* Date of last update of the offer on the destination website (API)
*/
Date upDate;
/*
* Date of last check if offer is still valid
*/
Date checkDate;
/**
* Creates OfferDTO instance.
*
* @param newOfferId Internal Offer identifier
*/
public OfferDTO(final String newOfferId) {
this.offerId = newOfferId;
}
}

View File

@@ -1,13 +1,25 @@
package de.rwu.easydrop.core; package de.rwu.easydrop.core;
import de.rwu.easydrop.model.Offer;
import java.util.List; import java.util.List;
import de.rwu.easydrop.data.connector.AbstractProductPersistence;
import de.rwu.easydrop.model.Offer;
import de.rwu.easydrop.service.retriever.ProductRetriever;
import de.rwu.easydrop.service.writer.OfferWriter;
public class OfferProvisioner { public class OfferProvisioner {
ProductRetriever productRetriever;
// ProductWriter
AbstractProductPersistence db;
OfferWriter offerWriter;
public OfferProvisioner(/*OfferWriter for database? */) { public OfferProvisioner(/*OfferWriter for database? */) {
//in die Klasse hardcoden, "webshop" nicht verwenden
//oder eine Konstante anlegen die das halten würde (besser skalierbar)
//die Konstante in model anlegen (in Product, Konstante valid origins)
this.offerWriter = new OfferWriter();
} }
public void runProvisioner(List<Offer> offersToProvision) { public void runProvisioner(List<Offer> offersToProvision) {
@@ -16,6 +28,20 @@ public class OfferProvisioner {
* Stellt Angebote bei der Zielplattform ein * Stellt Angebote bei der Zielplattform ein
* Schreibt eingestellte Angebote in die Datenbank * Schreibt eingestellte Angebote in die Datenbank
*/ */
for(Offer newOffer: offersToProvision){
// if(newOffer.getSourceProduct().getDataOrigin().equals("ebay")){
// //put newOffer on amazon
// }
//else put newOffer on eBay
// if successfully transmitted
// add to persistence
// "duplicate" the product with dataOrigin new platform and merchant = "me"
offerWriter.writeOfferToPersistence(newOffer);
}

View File

@@ -0,0 +1,28 @@
package de.rwu.easydrop.data.connector;
import de.rwu.easydrop.api.dto.OfferDTO;
public abstract class AbstractOfferPersistence {
/**
* Data origin.
*/
public static final String DATA_ORIGIN = "Persistence";
/**
* Writes a ProductDTO to persistence.
*
* @param dto
*/
public abstract void saveOffer(OfferDTO dto);
/**
* Gets a OfferDTO from persistence.
*/
//@Override
public abstract OfferDTO getOfferDTOById(String OfferId);
/**
* Deletes all data from persistence.
*/
public abstract void clearData();
}

View File

@@ -39,4 +39,9 @@ public class Offer {
*/ */
Date checkDate; Date checkDate;
/*
* ID of the offer
*/
String offerId;
} }

View File

@@ -0,0 +1,50 @@
package de.rwu.easydrop.service.mapping;
import de.rwu.easydrop.api.dto.OfferDTO;
import de.rwu.easydrop.model.Offer;
/**
* Maps between Offer, OfferDTO and OfferDTO.
*
* @since 0.2.0
*
* @see Offer
* @see OfferDTO
* @see OfferDTO
*/
public final class OfferMapper {
/**
* Private constructor to prevent unwanted instantiation.
*
* @throws UnsupportedOperationException always
*/
private OfferMapper() throws UnsupportedOperationException {
throw new UnsupportedOperationException("This is a mapping class, don't instantiate it.");
}
/**
* Creates a Offer object from a corresponding DTO.
*
* @param dto Offer Data Transfer Object
* @return Offer
*/
public static Offer mapOfferFromDTO(final OfferDTO dto) {
Offer offer = new Offer();
return offer;
}
/**
* Creates an OfferDTO object from a corresponding offer.
*
* @param Offer offer
* @return OfferDTO
*/
public static OfferDTO mapOfferToDTO(final Offer offer) {
OfferDTO dto = new OfferDTO(offer.getOfferId());
return dto;
}
}

View File

@@ -0,0 +1,32 @@
package de.rwu.easydrop.service.validation;
import java.util.HashSet;
import java.util.Set;
import de.rwu.easydrop.exception.InvalidProductException;
import de.rwu.easydrop.model.Product;
import de.rwu.easydrop.model.Offer;
/**
* Confirms validity of Offer data.
*
* @since 0.2.0
*/
public class OfferValidator {
/**
* Private constructor to prevent unwanted instantiation.
*
* @throws UnsupportedOperationException always
*/
private OfferValidator() throws UnsupportedOperationException {
throw new UnsupportedOperationException("This is a validator class, don't instantiate it.");
}
/**
* Makes sure an Offer does not contain invalid information.
*
* @param offer the Offer
*/
public static void validate(final Offer offer) {}
}

View File

@@ -0,0 +1,33 @@
package de.rwu.easydrop.service.writer;
import de.rwu.easydrop.api.dto.OfferDTO;
import de.rwu.easydrop.data.connector.AbstractOfferPersistence;
import de.rwu.easydrop.model.Offer;
import de.rwu.easydrop.service.mapping.OfferMapper;
import de.rwu.easydrop.service.validation.OfferValidator;
public class OfferWriter {
/**
* Persistence.
*/
private AbstractOfferPersistence persistence;
/**
* @param newPersistence the persistence to set
*/
public void setPersistence(final AbstractOfferPersistence newPersistence) {
this.persistence = newPersistence;
}
/**
* Validates and saves product to persistence.
*
* @param product
*/
public void writeOfferToPersistence(final Offer offer) {
OfferValidator.validate(offer);
OfferDTO dto = OfferMapper.mapOfferToDTO(offer);
persistence.saveOffer(dto);
}
}