Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 9ffa82d3

Přidáno uživatelem Jakub Šmíd před téměř 3 roky(ů)

Added catalog filter
re #9164

Zobrazit rozdíly:

backend/src/main/java/cz/zcu/kiv/backendapi/catalog/CatalogController.java
27 27
    }
28 28

  
29 29
    /**
30
     * Returns catalog
30
     * Returns catalog entries satisfying given filter
31 31
     *
32
     * @return catalog
32
     * @param name    name - optional
33
     * @param country country - optional
34
     * @param type    type - optional
35
     * @return list of catalog entries satisfying given filter
33 36
     */
34 37
    @GetMapping("")
35
    public ResponseEntity<List<CatalogDto>> getAllUsers() {
36
        return new ResponseEntity<>(catalogService.getCatalog(), HttpStatus.OK);
38
    public ResponseEntity<List<CatalogDto>> getCatalog(@RequestParam(defaultValue = "") String name, @RequestParam(defaultValue = "") String country, @RequestParam(defaultValue = "") String type) {
39
        return new ResponseEntity<>(catalogService.getCatalog(name, country, type), HttpStatus.OK);
37 40
    }
38 41

  
39 42
    /**
backend/src/main/java/cz/zcu/kiv/backendapi/catalog/CatalogDto.java
1 1
package cz.zcu.kiv.backendapi.catalog;
2 2

  
3
import com.fasterxml.jackson.annotation.JsonProperty;
4
import lombok.AllArgsConstructor;
3 5
import lombok.Data;
6
import lombok.NoArgsConstructor;
4 7

  
5 8
import java.util.Collections;
6 9
import java.util.Set;
......
10 13
 * Class representing catalog DTO
11 14
 */
12 15
@Data
16
@AllArgsConstructor
17
@NoArgsConstructor
13 18
public class CatalogDto {
14 19
    /**
15 20
     * Id
16 21
     */
22
    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
17 23
    private UUID id;
18 24

  
19 25
    /**
backend/src/main/java/cz/zcu/kiv/backendapi/catalog/CatalogEntity.java
62 62
     */
63 63
    @OneToMany(mappedBy = "catalog", cascade = CascadeType.ALL)
64 64
    @LazyCollection(LazyCollectionOption.FALSE)
65
    private Set<BibliographyEntity> bibliography;
65
    private Set<BibliographyEntity> bibliography = Collections.emptySet();
66 66

  
67 67
    /**
68 68
     * Countries
69 69
     */
70 70
    @OneToMany(mappedBy = "catalog", cascade = CascadeType.ALL)
71 71
    @LazyCollection(LazyCollectionOption.FALSE)
72
    private Set<CountryEntity> countries;
72
    private Set<CountryEntity> countries = Collections.emptySet();
73 73

  
74 74
    /**
75 75
     * Written forms
76 76
     */
77 77
    @OneToMany(mappedBy = "catalog", cascade = CascadeType.ALL)
78 78
    @LazyCollection(LazyCollectionOption.FALSE)
79
    private Set<WrittenFormEntity> writtenForms;
79
    private Set<WrittenFormEntity> writtenForms = Collections.emptySet();
80 80

  
81 81
    /**
82 82
     * Alternative names
83 83
     */
84 84
    @OneToMany(mappedBy = "catalog", cascade = CascadeType.ALL)
85 85
    @LazyCollection(LazyCollectionOption.FALSE)
86
    private Set<AlternativeNameEntity> alternativeNames;
86
    private Set<AlternativeNameEntity> alternativeNames = Collections.emptySet();
87 87

  
88 88
    /**
89 89
     * Set of user roles - many-to-many relationship
backend/src/main/java/cz/zcu/kiv/backendapi/catalog/CatalogRepository.java
5 5
import org.springframework.stereotype.Repository;
6 6
import org.springframework.transaction.annotation.Transactional;
7 7

  
8
import java.util.List;
9 8
import java.util.Set;
10 9
import java.util.UUID;
11 10

  
......
17 16
public interface CatalogRepository extends JpaRepository<CatalogEntity, UUID> {
18 17

  
19 18
    /**
20
     * Selects all catalog entries with given name or where given name appears in alternative names
21
     * @param name name
22
     * @return list of catalog entries with given name / alternative name
19
     * Returns all catalog entries containing specific values
20
     *
21
     * @param name    name - optional
22
     * @param country country - optional
23
     * @param type    type - optional
24
     * @return set of catalog entries satisfying filter conditions
23 25
     */
24
    @Query("SELECT DISTINCT c FROM CatalogEntity c LEFT JOIN AlternativeNameEntity a ON c = a.catalog WHERE (?1 = c.name OR ?1 = a.name)")
25
    List<CatalogEntity> getCatalogEntitiesByName(String name);
26

  
27
//    @Query("SELECT DISTINCT c FROM CatalogEntity c LEFT JOIN AlternativeNameEntity a ON c = a.catalog WHERE (?1 IS NULL OR ?1 = c.name OR ?1 = a.name)" +
28
//            "LEFT JOIN TypeEntity e on c = e.catalog WHERE (?2 IS NULL OR e.)")
29
//    Set<CatalogEntity> filterCatalog(String name, String type, String country);
26
    @Query("SELECT DISTINCT e FROM CatalogEntity e LEFT JOIN AlternativeNameEntity a ON e = a.catalog " +
27
            "LEFT JOIN CountryEntity c ON e = c.catalog " +
28
            "INNER JOIN e.types t " +
29
            "WHERE (?1 = '' OR e.name LIKE ?1 OR a.name LIKE ?1) " +
30
            "AND (?2 = '' OR c.name LIKE ?2) " +
31
            "AND (?3 = '' OR t.type LIKE ?3)")
32
    Set<CatalogEntity> filterCatalog(String name, String country, String type);
30 33
}
backend/src/main/java/cz/zcu/kiv/backendapi/catalog/CatalogServiceImpl.java
3 3
import cz.zcu.kiv.backendapi.alternativename.AlternativeNameEntity;
4 4
import cz.zcu.kiv.backendapi.bibliography.BibliographyEntity;
5 5
import cz.zcu.kiv.backendapi.country.CountryEntity;
6
import cz.zcu.kiv.backendapi.exception.ApiRequestException;
6 7
import cz.zcu.kiv.backendapi.type.TypeEntity;
7 8
import cz.zcu.kiv.backendapi.type.ITypeService;
8 9
import cz.zcu.kiv.backendapi.writtenform.WrittenFormEntity;
9 10
import lombok.RequiredArgsConstructor;
10 11
import lombok.extern.slf4j.Slf4j;
11
import org.springframework.security.core.userdetails.UsernameNotFoundException;
12
import org.springframework.http.HttpStatus;
12 13
import org.springframework.stereotype.Service;
13 14
import org.springframework.transaction.annotation.Transactional;
14 15

  
15 16
import java.util.List;
17
import java.util.Set;
16 18
import java.util.UUID;
17 19
import java.util.stream.Collectors;
18 20

  
......
56 58
    public void updateCatalogEntry(UUID id, CatalogDto catalogDto) {
57 59
        CatalogEntity catalogEntity = catalogRepository.findById(id).orElseThrow(() -> {
58 60
            log.error(CATALOG_ENTRY_NOT_FOUND);
59
            throw new UsernameNotFoundException(CATALOG_ENTRY_NOT_FOUND);
61
            throw new ApiRequestException(CATALOG_ENTRY_NOT_FOUND, HttpStatus.NOT_FOUND);
60 62
        });
61 63
        convertDtoToEntity(catalogDto, catalogEntity);
62
        catalogRepository.save(catalogEntity);
64
        saveCatalogEntity(catalogEntity);
63 65
        log.info("Catalog entry updated");
64 66
    }
65 67

  
......
67 69
    public void deleteCatalogEntry(UUID id) {
68 70
        CatalogEntity catalogEntity = catalogRepository.findById(id).orElseThrow(() -> {
69 71
            log.error(CATALOG_ENTRY_NOT_FOUND);
70
            throw new UsernameNotFoundException(CATALOG_ENTRY_NOT_FOUND);
72
            throw new ApiRequestException(CATALOG_ENTRY_NOT_FOUND, HttpStatus.NOT_FOUND);
71 73
        });
72 74
        catalogRepository.delete(catalogEntity);
73 75
        log.info("Catalog entry deleted");
74 76
    }
75 77

  
76 78
    @Override
77
    public List<CatalogDto> getCatalog() {
79
    public List<CatalogDto> getCatalog(String name, String country, String type) {
78 80
        log.info("Retrieving catalog");
79
        List<CatalogEntity> entities = catalogRepository.findAll();
81
        Set<CatalogEntity> entities = catalogRepository.filterCatalog(name, country, type);
80 82
        return entities.stream().map(this::convertEntityToDto).collect(Collectors.toList());
81 83
    }
82 84

  
backend/src/main/java/cz/zcu/kiv/backendapi/catalog/ICatalogService.java
37 37
    void deleteCatalogEntry(UUID id);
38 38

  
39 39
    /**
40
     * Returns whole catalog
40
     * Returns catalog entries satisfying given filter
41 41
     *
42
     * @return catalog (as list of catalog entries)
42
     * @param name    name - optional
43
     * @param country country - optional
44
     * @param type    type - optional
45
     * @return list of catalog entries satisfying given filter
43 46
     */
44
    List<CatalogDto> getCatalog();
47
    List<CatalogDto> getCatalog(String name, String country, String type);
45 48

  
46 49
}

Také k dispozici: Unified diff