Skip to content

Commit cb0c7f4

Browse files
author
chedim
committed
redirect / to swagger ui
1 parent 49748df commit cb0c7f4

File tree

4 files changed

+44
-13
lines changed

4 files changed

+44
-13
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package trycb.controller;
2+
/**
3+
* @author : chedim (chedim@couchbaser)
4+
* @file : IndexController
5+
* @created : Friday Mar 18, 2022 09:54:39 EDT
6+
*/
7+
8+
import org.springframework.http.HttpHeaders;
9+
import org.springframework.http.HttpStatus;
10+
import org.springframework.http.ResponseEntity;
11+
import org.springframework.web.bind.annotation.GetMapping;
12+
import org.springframework.web.bind.annotation.RestController;
13+
14+
@RestController
15+
public class IndexController {
16+
@GetMapping("/")
17+
public ResponseEntity<Void> index() {
18+
HttpHeaders headers = new HttpHeaders();
19+
headers.add("Location", "/swagger-ui/index.html");
20+
return new ResponseEntity<Void>(headers, HttpStatus.FOUND);
21+
}
22+
}
23+

src/main/java/trycb/controller/ProfileController.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import java.util.List;
44
import java.util.UUID;
55

6-
import com.couchbase.client.core.error.DocumentNotFoundException;
7-
86
import org.slf4j.Logger;
97
import org.slf4j.LoggerFactory;
108
import org.springframework.beans.factory.annotation.Autowired;
@@ -30,14 +28,22 @@ public class ProfileController {
3028
private ProfileRepository profileRepository;
3129

3230
@GetMapping("/profile")
33-
public ResponseEntity<List<Profile>> listProfiles(@RequestParam(required = false) String query, @RequestParam(defaultValue = "10") int pageSize, @RequestParam(defaultValue = "0") int page) {
31+
public ResponseEntity<List<Profile>> listProfiles(
32+
@RequestParam(required = false) String query,
33+
@RequestParam(defaultValue = "10") int pageSize,
34+
@RequestParam(defaultValue = "0") int page
35+
) {
3436
if (pageSize < 1 || pageSize > 10) pageSize = 10;
3537
PageRequest pageRequest = PageRequest.of(page, pageSize);
3638
List<Profile> result;
37-
if (query != null && query.length() > 0) {
38-
result = profileRepository.findByQuery(query, pageRequest).toList();
39-
} else {
39+
40+
if (query == null || query.length() != 0) {
4041
result = profileRepository.findAll(pageRequest).toList();
42+
} else {
43+
// This is just a LIKE query.
44+
// For full-text search documentation refer to:
45+
// https://docs.couchbase.com/java-sdk/current/howtos/full-text-searching-with-sdk.html
46+
result = profileRepository.findByText(query, pageRequest).toList();
4147
}
4248

4349
if (result != null && result.size() > 0) {
@@ -61,6 +67,7 @@ public ResponseEntity<Profile> getProfileById(@PathVariable("id") UUID id) {
6167

6268
@PostMapping("/profile")
6369
public ResponseEntity<Profile> saveProfile(@RequestBody Profile profile) {
70+
// the same endpoint can be used to create and save the object
6471
profile = profileRepository.save(profile);
6572
return ResponseEntity.status(HttpStatus.CREATED).body(profile);
6673
}
@@ -75,9 +82,4 @@ public ResponseEntity<Void> deleteProfile(@PathVariable UUID id) {
7582
return ResponseEntity.notFound().build();
7683
}
7784
}
78-
79-
@GetMapping("/test/**")
80-
public ResponseEntity<String> test() {
81-
return ResponseEntity.ok("test");
82-
}
8385
}

src/main/java/trycb/model/Profile.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@
33
import java.util.UUID;
44

55
import org.springframework.data.annotation.Id;
6+
import org.springframework.data.couchbase.repository.Collection;
7+
import org.springframework.data.couchbase.repository.Scope;
68

9+
@Scope("_default")
10+
@Collection("profile")
711
public class Profile {
8-
private @Id UUID id;
12+
@Id
13+
@GeneratedValue
14+
private UUID id;
915
private String firstName, lastName;
1016
private byte age;
1117
private String address;

src/main/java/trycb/repository/ProfileRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
@Repository
1515
public interface ProfileRepository extends PagingAndSortingRepository<Profile, UUID> {
1616
@Query("#{#n1ql.selectEntity} WHERE firstName LIKE '%' || $1 || '%' OR lastName LIKE '%' || $1 || '%' OR address LIKE '%' || $1 || '%'")
17-
Page<Profile> findByQuery(String query, Pageable pageable);
17+
Page<Profile> findByText(String query, Pageable pageable);
1818

1919
Page<Profile> findByAge(byte age, Pageable pageable);
2020
}

0 commit comments

Comments
 (0)