Skip to content

Commit

Permalink
improved creation of eventInfo and fixed adding pictures
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Jun 1, 2023
1 parent e1f85c7 commit dbfeb5f
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 17 deletions.
15 changes: 7 additions & 8 deletions src/main/java/at/fh/joanneum/irfc/service/EventInfoService.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,18 @@ public EventInfoDTO update(Long id, EventInfoDTO eventInfoDTO) {
private void setValues(EventInfoDTO eventInfoDTO, EventInfoEntity newEntity) {
newEntity.setInfoText(eventInfoDTO.getInfoText());

Set<PictureEntity> pictures;
if(newEntity.getPictures() != null) {
pictures = newEntity.getPictures();
} else {
Set<PictureEntity> pictures = newEntity.getPictures();
if (pictures == null) {
pictures = new HashSet<>();
}

for(PictureDTO picture : eventInfoDTO.getPictures()) {
for (PictureDTO picture : eventInfoDTO.getPictures()) {
Optional<PictureEntity> pictureOptional = this.pictureRepository.findByIdOptional(picture.getPictureId());
if(pictureOptional.isEmpty()){
throw new RuntimeException("no Picture with id "+ picture.getPictureId());
if (pictureOptional.isEmpty()) {
throw new RuntimeException("No Picture with id " + picture.getPictureId());
} else {
PictureEntity pictureEntity = PictureMapper.INSTANCE.toEntity(picture);
PictureEntity pictureEntity = pictureOptional.get();
pictureEntity.setEventInfo(newEntity);
pictures.add(pictureEntity);
}
}
Expand Down
18 changes: 12 additions & 6 deletions src/main/java/at/fh/joanneum/irfc/service/EventService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import at.fh.joanneum.irfc.model.event.EventDTO;
import at.fh.joanneum.irfc.model.event.EventMapper;
import at.fh.joanneum.irfc.model.eventInfo.EventInfoDTO;
import at.fh.joanneum.irfc.persistence.entiy.*;
import at.fh.joanneum.irfc.persistence.repository.*;

Expand Down Expand Up @@ -31,8 +32,10 @@ public class EventService {
EventInfoRepository eventInfoRepository;
@Inject
EventCategoryRepository eventCategoryRepository;
@Inject
EventInfoService eventInfoService;

public List<EventDTO> getAll() {
public List<EventDTO> getAll() { //TODO throws exception (pls fix)
return eventRepository.listAll().stream()
.map(EventMapper.INSTANCE::toDto)
.collect(Collectors.toUnmodifiableList());
Expand All @@ -47,7 +50,6 @@ public EventDTO get(Long id) {

@Transactional
public EventDTO create(EventDTO eventDTO) {

checkDTOvalues(eventDTO);

EventEntity newEntity = new EventEntity();
Expand Down Expand Up @@ -126,12 +128,16 @@ private void setValues(EventDTO eventDTOCreate, EventEntity newEntity) {
}

if(eventDTOCreate.getEventInfo() != null) {
Optional<EventInfoEntity> eventInfoOptional = this.eventInfoRepository.findByIdOptional(eventDTOCreate.getEventInfo().getEventInfoId());
Long eventInfoId = eventDTOCreate.getEventInfo().getEventInfoId();
if(isNull(eventInfoId)) {
eventInfoId = 0l;
}
Optional<EventInfoEntity> eventInfoOptional = this.eventInfoRepository.findByIdOptional(eventInfoId);
if (eventInfoOptional.isEmpty()) {
throw new RuntimeException("no EventInfo with id " + eventDTOCreate.getEventInfo().getEventInfoId());
} else {
newEntity.setEventInfo(eventInfoOptional.get());
EventInfoDTO eventInfoDto= this.eventInfoService.create(eventDTOCreate.getEventInfo());
eventInfoOptional = this.eventInfoRepository.findByIdOptional(eventInfoDto.getEventInfoId());
}
newEntity.setEventInfo(eventInfoOptional.get());
} else {
throw new RuntimeException("no EventInfo");
}
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/at/fh/joanneum/irfc/service/PictureService.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public class PictureService {
@ConfigProperty(name="pictures.root_path")
String pictureRootPath;

@ConfigProperty(name="pictures.url") //TODO replace this with an .env var
String pictureUrl;

public PictureDTO get(Long id) {
Optional<PictureEntity> byIdOptional = pictureRepository.findByIdOptional(id);
if(byIdOptional.isEmpty()){
Expand Down Expand Up @@ -83,7 +86,7 @@ public void delete(Long id) {
try
{

File f= new File(pictureRootPath + pictureDto.getPath());
File f= new File(pictureDto.getPath().replace(pictureUrl, pictureRootPath));
if(!f.delete())
{
throw new RuntimeException("Picture with Path " + pictureDto.getPath() + " could not be deleted");
Expand All @@ -107,11 +110,12 @@ public PictureDTO create(MultipartBody data) {
PictureEntity newEntity = new PictureEntity();

try {
File targetFile = new File(pictureRootPath + UUID.randomUUID() + "." + data.getFileEndingType().name().toLowerCase());
String fileName = UUID.randomUUID() + "." + data.getFileEndingType().name().toLowerCase();
File targetFile = new File(pictureRootPath + fileName);
OutputStream outStream = new FileOutputStream(targetFile);
outStream.write(data.file.readAllBytes());
outStream.close();
pictureDTO.setPath(pictureRootPath + targetFile.getPath());
pictureDTO.setPath(pictureUrl + fileName);
} catch (IOException e) {
throw new RuntimeException("Error storing Image");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public VotingDTO endVoting(Long id) {
}
byId.setActive(false);
VotingResultEntity votingResult = new VotingResultEntity();
votingResult.setTitle(byId.getTitle());
votingResult.setVoting(byId);
votingResult.setEndDate(Instant.now().toEpochMilli()); //TODO check if this is correct
votingResultRepository.persist(votingResult);
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ quarkus:

pictures:
root_path: ${PICTURES_ROOT_PATH:C:/temp/pics/}
url: https://tmp.at/


# container-image:
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/db/53-add-fields.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,9 @@
constraintName="fk_voting_event_event" onDelete="CASCADE"/>

<addUniqueConstraint tableName="voting_event" columnNames="voting_id, event_id"/>

<addUniqueConstraint columnNames="event_id, fk_event_info"
constraintName="event_info_constraint"
tableName="event"/>
</changeSet>
</databaseChangeLog>

0 comments on commit dbfeb5f

Please sign in to comment.