Commit 5ced9f55 by Patryk Czarnik

jeszcze dodatkowe metody

parent 4801701a
......@@ -7,6 +7,7 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import sklep.db.DBException;
import sklep.db.DBSettings;
......@@ -46,6 +47,16 @@ public class PhotoUtil {
}
}
public static void writeBytes(int productId, byte[] bytes) {
try {
Path path = getPath(productId);
Files.write(path, bytes, StandardOpenOption.CREATE);
} catch (Exception e) {
// wypisujemy błąd, ale metoda kończy się normalnie
e.printStackTrace();
}
}
private static Path getPath(int productId) throws DBException {
String dir = DBSettings.load().getProperty("photo_dir");
String fileName = productId + EXT;
......
......@@ -6,10 +6,14 @@ import java.util.List;
import jakarta.jws.WebParam;
import jakarta.jws.WebResult;
import jakarta.jws.WebService;
import sklep.db.CustomerDAO;
import sklep.db.DBConnection;
import sklep.db.DBException;
import sklep.db.OrderDAO;
import sklep.db.ProductDAO;
import sklep.db.RecordNotFound;
import sklep.model.Customer;
import sklep.model.Order;
import sklep.model.Product;
@WebService
......@@ -50,8 +54,29 @@ public class Sklep {
}
}
@WebResult(name="customer")
public Customer oneCustomer(@WebParam(name="email") String email) throws DBException, RecordNotFound {
try(DBConnection db = DBConnection.open()) {
CustomerDAO customerDAO = db.customerDAO();
return customerDAO.findByEmail(email);
}
}
@WebResult(name="order")
public Order oneOrder(@WebParam(name="id") int orderId) throws DBException, RecordNotFound {
try(DBConnection db = DBConnection.open()) {
OrderDAO orderDAO = db.orderDAO();
return orderDAO.findById(orderId);
}
}
@WebResult(name="bytes")
public byte[] getPhoto(@WebParam(name="id") int productId) throws DBException, RecordNotFound {
return PhotoUtil.readBytes(productId);
}
public void savePhoto(@WebParam(name="id") int productId,
@WebParam(name="bytes") byte[] bytes) {
PhotoUtil.writeBytes(productId, bytes);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment