Commit 1b19d0d4 authored by Norbert Komiňák's avatar Norbert Komiňák
Browse files

Moved Dao Exception from Service to Dao layer.

parent c56faef8
...@@ -2,6 +2,7 @@ package cz.fi.muni.pa165.flea.market.manager.dao; ...@@ -2,6 +2,7 @@ package cz.fi.muni.pa165.flea.market.manager.dao;
import com.mysema.query.jpa.impl.JPAQuery; import com.mysema.query.jpa.impl.JPAQuery;
import cz.fi.muni.pa165.flea.market.manager.dao.domain.DomainDaoImpl; import cz.fi.muni.pa165.flea.market.manager.dao.domain.DomainDaoImpl;
import cz.fi.muni.pa165.flea.market.manager.dao.exception.DaoException;
import cz.fi.muni.pa165.flea.market.manager.entity.MarketEvent; import cz.fi.muni.pa165.flea.market.manager.entity.MarketEvent;
import cz.fi.muni.pa165.flea.market.manager.entity.QMarketEvent; import cz.fi.muni.pa165.flea.market.manager.entity.QMarketEvent;
import lombok.NonNull; import lombok.NonNull;
...@@ -22,10 +23,14 @@ public class MarketEventDao extends DomainDaoImpl<MarketEvent, QMarketEvent> { ...@@ -22,10 +23,14 @@ public class MarketEventDao extends DomainDaoImpl<MarketEvent, QMarketEvent> {
} }
public List<MarketEvent> findAllByMarketLocation(@NonNull String marketLocationId) { public List<MarketEvent> findAllByMarketLocation(@NonNull String marketLocationId) {
try {
JPAQuery query = new JPAQuery(entityManager); JPAQuery query = new JPAQuery(entityManager);
return query return query
.from(qEntity) .from(qEntity)
.where(qEntity.location.id.eq(marketLocationId)) .where(qEntity.location.id.eq(marketLocationId))
.list(qEntity); .list(qEntity);
} catch (Exception e) {
throw new DaoException(e.getMessage(), e);
}
} }
} }
...@@ -2,13 +2,12 @@ package cz.fi.muni.pa165.flea.market.manager.dao; ...@@ -2,13 +2,12 @@ package cz.fi.muni.pa165.flea.market.manager.dao;
import com.mysema.query.jpa.impl.JPAQuery; import com.mysema.query.jpa.impl.JPAQuery;
import cz.fi.muni.pa165.flea.market.manager.dao.domain.DomainDaoImpl; import cz.fi.muni.pa165.flea.market.manager.dao.domain.DomainDaoImpl;
import cz.fi.muni.pa165.flea.market.manager.dao.exception.DaoException;
import cz.fi.muni.pa165.flea.market.manager.entity.MarketLocation; import cz.fi.muni.pa165.flea.market.manager.entity.MarketLocation;
import cz.fi.muni.pa165.flea.market.manager.entity.QMarketLocation; import cz.fi.muni.pa165.flea.market.manager.entity.QMarketLocation;
import lombok.NonNull; import lombok.NonNull;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.List;
/** /**
* DAO for entity {@link MarketLocation} * DAO for entity {@link MarketLocation}
* *
...@@ -22,10 +21,14 @@ public class MarketLocationDao extends DomainDaoImpl<MarketLocation, QMarketLoca ...@@ -22,10 +21,14 @@ public class MarketLocationDao extends DomainDaoImpl<MarketLocation, QMarketLoca
} }
public MarketLocation findByAddress(@NonNull String address) { public MarketLocation findByAddress(@NonNull String address) {
try {
JPAQuery query = new JPAQuery(entityManager); JPAQuery query = new JPAQuery(entityManager);
return query return query
.from(qEntity) .from(qEntity)
.where(qEntity.address.eq(address)) .where(qEntity.address.eq(address))
.singleResult(qEntity); .singleResult(qEntity);
} catch (Exception e) {
throw new DaoException(e.getMessage(), e);
}
} }
} }
...@@ -2,15 +2,13 @@ package cz.fi.muni.pa165.flea.market.manager.dao; ...@@ -2,15 +2,13 @@ package cz.fi.muni.pa165.flea.market.manager.dao;
import com.mysema.query.jpa.impl.JPAQuery; import com.mysema.query.jpa.impl.JPAQuery;
import cz.fi.muni.pa165.flea.market.manager.dao.domain.DomainDaoImpl; import cz.fi.muni.pa165.flea.market.manager.dao.domain.DomainDaoImpl;
import cz.fi.muni.pa165.flea.market.manager.dao.exception.DaoException;
import cz.fi.muni.pa165.flea.market.manager.entity.QStand; import cz.fi.muni.pa165.flea.market.manager.entity.QStand;
import cz.fi.muni.pa165.flea.market.manager.entity.Stand; import cz.fi.muni.pa165.flea.market.manager.entity.Stand;
import cz.fi.muni.pa165.flea.market.manager.entity.StandType;
import cz.fi.muni.pa165.flea.market.manager.enums.ItemCategory;
import lombok.NonNull; import lombok.NonNull;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.List; import java.util.List;
import java.util.Objects;
/** /**
* Dao for {@link Stand} entity that provides basic CRUD operations. * Dao for {@link Stand} entity that provides basic CRUD operations.
...@@ -25,27 +23,39 @@ public class StandDao extends DomainDaoImpl<Stand, QStand> { ...@@ -25,27 +23,39 @@ public class StandDao extends DomainDaoImpl<Stand, QStand> {
} }
public List<Stand> findAllBySellerId(@NonNull String sellerId) { public List<Stand> findAllBySellerId(@NonNull String sellerId) {
try {
JPAQuery query = new JPAQuery(entityManager); JPAQuery query = new JPAQuery(entityManager);
return query return query
.from(qEntity) .from(qEntity)
.where(qEntity.seller.id.eq(sellerId)) .where(qEntity.seller.id.eq(sellerId))
.list(qEntity); .list(qEntity);
} catch (Exception e) {
throw new DaoException(e.getMessage(), e);
}
} }
public List<Stand> findAllByEventId(@NonNull String eventId) { public List<Stand> findAllByEventId(@NonNull String eventId) {
try {
JPAQuery query = new JPAQuery(entityManager); JPAQuery query = new JPAQuery(entityManager);
return query return query
.from(qEntity) .from(qEntity)
.where(qEntity.event.id.eq(eventId)) .where(qEntity.event.id.eq(eventId))
.list(qEntity); .list(qEntity);
} catch (Exception e) {
throw new DaoException(e.getMessage(), e);
}
} }
public List<Stand> findAllByStandType(@NonNull String standTypeId) { public List<Stand> findAllByStandType(@NonNull String standTypeId) {
try {
JPAQuery query = new JPAQuery(entityManager); JPAQuery query = new JPAQuery(entityManager);
return query return query
.from(qEntity) .from(qEntity)
.where(qEntity.standType.id.eq(standTypeId)) .where(qEntity.standType.id.eq(standTypeId))
.list(qEntity); .list(qEntity);
} catch (Exception e) {
throw new DaoException(e.getMessage(), e);
}
} }
} }
package cz.fi.muni.pa165.flea.market.manager.dao; package cz.fi.muni.pa165.flea.market.manager.dao;
import com.mysema.query.jpa.impl.JPAQuery;
import cz.fi.muni.pa165.flea.market.manager.dao.domain.DomainDaoImpl; import cz.fi.muni.pa165.flea.market.manager.dao.domain.DomainDaoImpl;
import cz.fi.muni.pa165.flea.market.manager.entity.QStandType; import cz.fi.muni.pa165.flea.market.manager.entity.QStandType;
import cz.fi.muni.pa165.flea.market.manager.entity.StandType; import cz.fi.muni.pa165.flea.market.manager.entity.StandType;
import lombok.NonNull;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
/** /**
......
package cz.fi.muni.pa165.flea.market.manager.dao; package cz.fi.muni.pa165.flea.market.manager.dao;
import cz.fi.muni.pa165.flea.market.manager.dao.domain.DomainDaoImpl;
import com.mysema.query.jpa.impl.JPAQuery; import com.mysema.query.jpa.impl.JPAQuery;
import cz.fi.muni.pa165.flea.market.manager.dao.domain.DomainDaoImpl;
import cz.fi.muni.pa165.flea.market.manager.dao.exception.DaoException;
import cz.fi.muni.pa165.flea.market.manager.entity.QUser; import cz.fi.muni.pa165.flea.market.manager.entity.QUser;
import cz.fi.muni.pa165.flea.market.manager.entity.User; import cz.fi.muni.pa165.flea.market.manager.entity.User;
import lombok.NonNull; import lombok.NonNull;
...@@ -19,10 +20,14 @@ public class UserDao extends DomainDaoImpl<User, QUser> { ...@@ -19,10 +20,14 @@ public class UserDao extends DomainDaoImpl<User, QUser> {
} }
public User findByEmail(@NonNull String email) { public User findByEmail(@NonNull String email) {
try {
JPAQuery query = new JPAQuery(entityManager); JPAQuery query = new JPAQuery(entityManager);
return query return query
.from(qEntity) .from(qEntity)
.where(qEntity.email.eq(email)) .where(qEntity.email.eq(email))
.singleResult(qEntity); .singleResult(qEntity);
} catch (Exception e) {
throw new DaoException(e.getMessage(), e);
}
} }
} }
...@@ -2,6 +2,7 @@ package cz.fi.muni.pa165.flea.market.manager.dao.domain; ...@@ -2,6 +2,7 @@ package cz.fi.muni.pa165.flea.market.manager.dao.domain;
import com.mysema.query.jpa.impl.JPAQuery; import com.mysema.query.jpa.impl.JPAQuery;
import com.mysema.query.types.path.EntityPathBase; import com.mysema.query.types.path.EntityPathBase;
import cz.fi.muni.pa165.flea.market.manager.dao.exception.DaoException;
import cz.fi.muni.pa165.flea.market.manager.entity.domain.DomainEntity; import cz.fi.muni.pa165.flea.market.manager.entity.domain.DomainEntity;
import lombok.NonNull; import lombok.NonNull;
import org.springframework.data.repository.NoRepositoryBean; import org.springframework.data.repository.NoRepositoryBean;
...@@ -30,28 +31,48 @@ public abstract class DomainDaoImpl<ENTITY extends DomainEntity, Q_ENTITY extend ...@@ -30,28 +31,48 @@ public abstract class DomainDaoImpl<ENTITY extends DomainEntity, Q_ENTITY extend
@Override @Override
public ENTITY create(@NonNull ENTITY object) { public ENTITY create(@NonNull ENTITY object) {
entityManager.persist(object); try {
return object; entityManager.persist(object);
return object;
} catch (Exception e) {
throw new DaoException(e.getMessage(), e);
}
} }
@Override @Override
public ENTITY findById(@NonNull String id) { public ENTITY findById(@NonNull String id) {
return entityManager.find(entityClass, id); try {
return entityManager.find(entityClass, id);
} catch (Exception e) {
throw new DaoException(e.getMessage(), e);
}
} }
@Override @Override
public List<ENTITY> findAll() { public List<ENTITY> findAll() {
JPAQuery query = new JPAQuery(entityManager); try {
return query.from(qEntity).list(qEntity); JPAQuery query = new JPAQuery(entityManager);
return query.from(qEntity).list(qEntity);
} catch (Exception e) {
throw new DaoException(e.getMessage(), e);
}
} }
@Override @Override
public ENTITY update(@NonNull ENTITY object) { public ENTITY update(@NonNull ENTITY object) {
return entityManager.merge(object); try {
return entityManager.merge(object);
} catch (Exception e) {
throw new DaoException(e.getMessage(), e);
}
} }
@Override @Override
public void delete(@NonNull ENTITY object) { public void delete(@NonNull ENTITY object) {
entityManager.remove(object); try {
entityManager.remove(object);
} catch (Exception e) {
throw new DaoException(e.getMessage(), e);
}
} }
} }
package cz.fi.muni.pa165.flea.market.manager.exception; package cz.fi.muni.pa165.flea.market.manager.dao.exception;
import org.springframework.dao.DataAccessException; import org.springframework.dao.DataAccessException;
......
...@@ -6,7 +6,6 @@ import cz.fi.muni.pa165.flea.market.manager.dto.marketlocation.MarketLocationCre ...@@ -6,7 +6,6 @@ import cz.fi.muni.pa165.flea.market.manager.dto.marketlocation.MarketLocationCre
import cz.fi.muni.pa165.flea.market.manager.dto.marketlocation.MarketLocationUpdateDTO; import cz.fi.muni.pa165.flea.market.manager.dto.marketlocation.MarketLocationUpdateDTO;
import cz.fi.muni.pa165.flea.market.manager.entity.MarketEvent; import cz.fi.muni.pa165.flea.market.manager.entity.MarketEvent;
import cz.fi.muni.pa165.flea.market.manager.entity.MarketLocation; import cz.fi.muni.pa165.flea.market.manager.entity.MarketLocation;
import cz.fi.muni.pa165.flea.market.manager.exception.DaoException;
import cz.fi.muni.pa165.flea.market.manager.service.domain.DomainServiceImpl; import cz.fi.muni.pa165.flea.market.manager.service.domain.DomainServiceImpl;
import lombok.NonNull; import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -48,11 +47,7 @@ public class MarketLocationService extends DomainServiceImpl<MarketLocation> { ...@@ -48,11 +47,7 @@ public class MarketLocationService extends DomainServiceImpl<MarketLocation> {
} }
public MarketLocation findByAddress(@NonNull String address) { public MarketLocation findByAddress(@NonNull String address) {
try { return locationDao.findByAddress(address);
return locationDao.findByAddress(address);
} catch (Exception e){
throw new DaoException(e.getMessage(), e);
}
} }
public MarketLocation constructLocationFromDTO(@NonNull MarketLocationCreateDTO dto){ public MarketLocation constructLocationFromDTO(@NonNull MarketLocationCreateDTO dto){
......
...@@ -7,14 +7,12 @@ import cz.fi.muni.pa165.flea.market.manager.dto.stand.StandDTO; ...@@ -7,14 +7,12 @@ import cz.fi.muni.pa165.flea.market.manager.dto.stand.StandDTO;
import cz.fi.muni.pa165.flea.market.manager.dto.stand.StandUpdateDTO; import cz.fi.muni.pa165.flea.market.manager.dto.stand.StandUpdateDTO;
import cz.fi.muni.pa165.flea.market.manager.entity.Stand; import cz.fi.muni.pa165.flea.market.manager.entity.Stand;
import cz.fi.muni.pa165.flea.market.manager.enums.ItemCategory; import cz.fi.muni.pa165.flea.market.manager.enums.ItemCategory;
import cz.fi.muni.pa165.flea.market.manager.exception.DaoException;
import cz.fi.muni.pa165.flea.market.manager.service.domain.DomainServiceImpl; import cz.fi.muni.pa165.flea.market.manager.service.domain.DomainServiceImpl;
import lombok.NonNull; import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -44,19 +42,11 @@ public class StandService extends DomainServiceImpl<Stand> { ...@@ -44,19 +42,11 @@ public class StandService extends DomainServiceImpl<Stand> {
} }
public List<Stand> findAllBySellerId(@NonNull String sellerId) { public List<Stand> findAllBySellerId(@NonNull String sellerId) {
try { return dao.findAllBySellerId(sellerId);
return dao.findAllBySellerId(sellerId);
} catch (Exception e) {
throw new DaoException(e.getMessage(), e);
}
} }
public List<Stand> findAllByEventId(@NonNull String eventId) { public List<Stand> findAllByEventId(@NonNull String eventId) {
try { return dao.findAllByEventId(eventId);
return dao.findAllByEventId(eventId);
} catch (Exception e) {
throw new DaoException(e.getMessage(), e);
}
} }
public Stand constructStandFromDTO(@NonNull StandCreateDTO standCreateDTO) { public Stand constructStandFromDTO(@NonNull StandCreateDTO standCreateDTO) {
......
...@@ -7,7 +7,6 @@ import cz.fi.muni.pa165.flea.market.manager.dto.standType.StandTypeDTO; ...@@ -7,7 +7,6 @@ import cz.fi.muni.pa165.flea.market.manager.dto.standType.StandTypeDTO;
import cz.fi.muni.pa165.flea.market.manager.dto.standType.StandTypeUpdateDTO; import cz.fi.muni.pa165.flea.market.manager.dto.standType.StandTypeUpdateDTO;
import cz.fi.muni.pa165.flea.market.manager.entity.Stand; import cz.fi.muni.pa165.flea.market.manager.entity.Stand;
import cz.fi.muni.pa165.flea.market.manager.entity.StandType; import cz.fi.muni.pa165.flea.market.manager.entity.StandType;
import cz.fi.muni.pa165.flea.market.manager.exception.DaoException;
import cz.fi.muni.pa165.flea.market.manager.service.domain.DomainServiceImpl; import cz.fi.muni.pa165.flea.market.manager.service.domain.DomainServiceImpl;
import lombok.NonNull; import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
......
...@@ -6,7 +6,6 @@ import cz.fi.muni.pa165.flea.market.manager.dto.user.UserDTO; ...@@ -6,7 +6,6 @@ import cz.fi.muni.pa165.flea.market.manager.dto.user.UserDTO;
import cz.fi.muni.pa165.flea.market.manager.dto.user.UserUpdateDTO; import cz.fi.muni.pa165.flea.market.manager.dto.user.UserUpdateDTO;
import cz.fi.muni.pa165.flea.market.manager.entity.User; import cz.fi.muni.pa165.flea.market.manager.entity.User;
import cz.fi.muni.pa165.flea.market.manager.enums.UserRole; import cz.fi.muni.pa165.flea.market.manager.enums.UserRole;
import cz.fi.muni.pa165.flea.market.manager.exception.DaoException;
import cz.fi.muni.pa165.flea.market.manager.service.domain.DomainServiceImpl; import cz.fi.muni.pa165.flea.market.manager.service.domain.DomainServiceImpl;
import lombok.NonNull; import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -34,11 +33,7 @@ public class UserService extends DomainServiceImpl<User> { ...@@ -34,11 +33,7 @@ public class UserService extends DomainServiceImpl<User> {
} }
public User findByEmail(@NonNull String email) { public User findByEmail(@NonNull String email) {
try { return dao.findByEmail(email);
return dao.findByEmail(email);
} catch (Exception e) {
throw new DaoException(e.getMessage(), e);
}
} }
public String hashPassword(String password) { public String hashPassword(String password) {
......
...@@ -2,7 +2,6 @@ package cz.fi.muni.pa165.flea.market.manager.service.domain; ...@@ -2,7 +2,6 @@ package cz.fi.muni.pa165.flea.market.manager.service.domain;
import cz.fi.muni.pa165.flea.market.manager.dao.domain.DomainDao; import cz.fi.muni.pa165.flea.market.manager.dao.domain.DomainDao;
import cz.fi.muni.pa165.flea.market.manager.entity.domain.DomainEntity; import cz.fi.muni.pa165.flea.market.manager.entity.domain.DomainEntity;
import cz.fi.muni.pa165.flea.market.manager.exception.DaoException;
import lombok.NonNull; import lombok.NonNull;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
...@@ -26,46 +25,26 @@ public abstract class DomainServiceImpl<ENTITY extends DomainEntity> implements ...@@ -26,46 +25,26 @@ public abstract class DomainServiceImpl<ENTITY extends DomainEntity> implements
@Override @Override
public ENTITY create(@NonNull ENTITY object) { public ENTITY create(@NonNull ENTITY object) {
try { return dao.create(object);
return dao.create(object);
} catch (Exception e){
throw new DaoException(e.getMessage(), e);
}
} }
@Override @Override
public ENTITY findById(@NonNull String id) { public ENTITY findById(@NonNull String id) {
try { return dao.findById(id);
return dao.findById(id);
} catch (Exception e){
throw new DaoException(e.getMessage(), e);
}
} }
@Override @Override
public List<ENTITY> findAll() { public List<ENTITY> findAll() {
try { return dao.findAll();
return dao.findAll();
} catch (Exception e){
throw new DaoException(e.getMessage(), e);
}
} }
@Override @Override
public ENTITY update(@NonNull ENTITY object) { public ENTITY update(@NonNull ENTITY object) {
try { return dao.update(object);
return dao.update(object);
} catch (Exception e){
throw new DaoException(e.getMessage(), e);
}
} }
@Override @Override
public void delete(@NonNull ENTITY object) { public void delete(@NonNull ENTITY object) {
try { dao.delete(object);
dao.delete(object);
} catch (Exception e){
throw new DaoException(e.getMessage(), e);
}
} }
} }
Supports Markdown
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