Skip to content

Commit

Permalink
Try to fix filtering by username
Browse files Browse the repository at this point in the history
  • Loading branch information
mraible committed May 9, 2024
1 parent dee922b commit f7be6cd
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
11 changes: 7 additions & 4 deletions src/main/java/org/jhipster/health/domain/Points.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.time.LocalDate;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

/**
* A Points.
Expand All @@ -30,23 +32,24 @@ public class Points implements Serializable {
private LocalDate date;

@Column(name = "exercise")
@org.springframework.data.elasticsearch.annotations.Field(type = org.springframework.data.elasticsearch.annotations.FieldType.Integer)
@Field(type = FieldType.Integer)
private Integer exercise;

@Column(name = "meals")
@org.springframework.data.elasticsearch.annotations.Field(type = org.springframework.data.elasticsearch.annotations.FieldType.Integer)
@org.springframework.data.elasticsearch.annotations.Field(type = FieldType.Integer)
private Integer meals;

@Column(name = "alcohol")
@org.springframework.data.elasticsearch.annotations.Field(type = org.springframework.data.elasticsearch.annotations.FieldType.Integer)
@org.springframework.data.elasticsearch.annotations.Field(type = FieldType.Integer)
private Integer alcohol;

@Size(max = 140)
@Column(name = "notes", length = 140)
@org.springframework.data.elasticsearch.annotations.Field(type = org.springframework.data.elasticsearch.annotations.FieldType.Text)
@org.springframework.data.elasticsearch.annotations.Field(type = FieldType.Text)
private String notes;

@ManyToOne(fetch = FetchType.LAZY)
@Field(type = FieldType.Nested, includeInParent = true)
private User user;

// jhipster-needle-entity-add-field - JHipster will add fields here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
/**
* Spring Data Elasticsearch repository for the {@link Points} entity.
*/
public interface PointsSearchRepository extends ElasticsearchRepository<Points, Long>, PointsSearchRepositoryInternal {}
public interface PointsSearchRepository extends ElasticsearchRepository<Points, Long>, PointsSearchRepositoryInternal {
Page<Points> findByUserLogin(String username, Pageable pageable);
}

interface PointsSearchRepositoryInternal {
Page<Points> search(String query, Pageable pageable);
Expand Down
16 changes: 6 additions & 10 deletions src/main/java/org/jhipster/health/web/rest/PointsResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -281,17 +281,13 @@ public ResponseEntity<List<Points>> searchPoints(
@org.springdoc.core.annotations.ParameterObject Pageable pageable
) {
log.debug("REST request to search for a page of Points for query {}", query);
if (SecurityUtils.isAuthenticated() && !SecurityUtils.hasCurrentUserThisAuthority(AuthoritiesConstants.ADMIN)) {
QueryVariant filterByUser = new MatchQuery.Builder()
.field("user.login")
.query(SecurityUtils.getCurrentUserLogin().orElse(""))
.build();
BoolQuery.Builder boolQueryBuilder = QueryBuilders.bool();
boolQueryBuilder.should(new Query(filterByUser));
query = new Query(boolQueryBuilder.build()).toString();
}
Page<Points> page;
try {
Page<Points> page = pointsSearchRepository.search(query, pageable);
if (SecurityUtils.isAuthenticated() && !SecurityUtils.hasCurrentUserThisAuthority(AuthoritiesConstants.ADMIN)) {
page = pointsSearchRepository.findByUserLogin(SecurityUtils.getCurrentUserLogin().orElse(""), pageable);
} else {
page = pointsSearchRepository.search(query, pageable);
}
HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(ServletUriComponentsBuilder.fromCurrentRequest(), page);
return ResponseEntity.ok().headers(headers).body(page.getContent());
} catch (RuntimeException e) {
Expand Down

0 comments on commit f7be6cd

Please sign in to comment.