-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Attachment type yes_or_no, Grouping attachments after single query load
- Loading branch information
Showing
7 changed files
with
109 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
begin transaction; | ||
|
||
-- set migration version | ||
update migration_version set version='20240222-2250'; | ||
|
||
-- Interne Downloads und Interne Tool-Downloads per Attachment | ||
alter type attachment_data_type add value 'yes_or_no'; | ||
insert into attachment_groups (code, label) values ('internal', 'Internes'); | ||
|
||
insert into attachment_types (code, label, attachment_group_id, datatype) values | ||
('internalfs', 'Fileserver', (select id from attachment_groups where code = 'internal'), 'yes_or_no'); | ||
|
||
insert into attachment_types (code, label, attachment_group_id, datatype) values | ||
('internalfs-tools', 'Fileserver Tools', (select id from attachment_groups where code = 'internal'), 'yes_or_no'); | ||
|
||
commit; |
13 changes: 13 additions & 0 deletions
13
src/main/java/de/holarse/backend/db/repositories/AttachmentGroupRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package de.holarse.backend.db.repositories; | ||
|
||
import de.holarse.backend.db.AttachmentGroup; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
|
||
public interface AttachmentGroupRepository extends JpaRepository<AttachmentGroup, Integer> { | ||
|
||
AttachmentGroup findByCode(final String code); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 5 additions & 2 deletions
7
src/main/java/de/holarse/backend/types/AttachmentDataType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
package de.holarse.backend.types; | ||
|
||
public enum AttachmentDataType { | ||
|
||
/* uri */ | ||
url, | ||
storage | ||
/* file path */ | ||
storage, | ||
/* yes or no */ | ||
yes_or_no | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/main/java/de/holarse/web/services/AttachmentService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package de.holarse.web.services; | ||
|
||
import de.holarse.backend.db.Article; | ||
import de.holarse.backend.db.Attachment; | ||
import de.holarse.backend.db.AttachmentGroup; | ||
import de.holarse.backend.db.AttachmentType; | ||
import de.holarse.backend.db.repositories.AttachmentRepository; | ||
import jakarta.validation.constraints.NotNull; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.context.annotation.RequestScope; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
@RequestScope | ||
public class AttachmentService { | ||
|
||
@Autowired | ||
private AttachmentRepository attachmentRepository; | ||
private Optional<Map<String, List<Attachment>>> nodeAttachmentGroups = Optional.empty(); | ||
|
||
protected Map<String, List<Attachment>> splitAttachments(final List<Attachment> attachments) { | ||
// TODO Sorting by weight | ||
return attachments.stream().collect(Collectors.groupingBy(a -> a.getAttachmentType().getAttachmentGroup().getCode())); | ||
} | ||
|
||
public List<Attachment> getAttachments(final Article article, final AttachmentGroup attachmentGroup) { | ||
if (article == null) { | ||
throw new IllegalArgumentException("article is null"); | ||
} | ||
if (attachmentGroup == null) { | ||
throw new IllegalArgumentException("attachmentGroup is null"); | ||
} | ||
|
||
if (nodeAttachmentGroups.isEmpty()) { | ||
// Daten laden und aufsplitten | ||
nodeAttachmentGroups = Optional.of(splitAttachments(attachmentRepository.findByNode(article.getNodeId()))); | ||
} | ||
|
||
return nodeAttachmentGroups.get().get(attachmentGroup.getCode()); | ||
} | ||
} |