package hirondelle.fish.main.resto;
import java.util.*;
import hirondelle.web4j.model.Id;
import hirondelle.web4j.database.DAOException;
import hirondelle.web4j.database.DuplicateException;
import hirondelle.web4j.util.Util;
import hirondelle.web4j.database.Db;
import static hirondelle.fish.main.resto.RestoAction.LIST_RESTOS;
import static hirondelle.fish.main.resto.RestoAction.FETCH_RESTO;
import static hirondelle.fish.main.resto.RestoAction.CHANGE_RESTO;
import static hirondelle.fish.main.resto.RestoAction.ADD_RESTO;
import static hirondelle.fish.main.resto.RestoAction.DELETE_RESTO;
final class RestoDAO {
List<Resto> list() throws DAOException {
return Db.list(Resto.class, LIST_RESTOS);
}
Resto fetch(Id aRestoId) throws DAOException {
return Db.fetch(Resto.class, FETCH_RESTO, aRestoId);
}
Id add(Resto aResto) throws DAOException, DuplicateException {
Id result = Db.add(ADD_RESTO, baseParamsFrom(aResto));
return result;
}
boolean change(Resto aResto) throws DAOException, DuplicateException {
int result = Db.edit(CHANGE_RESTO, Db.addIdTo(baseParamsFrom(aResto), aResto.getId()) );
return Util.isSuccess(result);
}
void delete(Id aRestoId) throws DAOException {
Db.delete(DELETE_RESTO, aRestoId);
}
private Object[] baseParamsFrom(Resto aResto){
return new Object[]{
aResto.getName(), aResto.getLocation(), aResto.getPrice(), aResto.getComment()
};
}
}