Skip to content

Commit

Permalink
[enhancement] JCommon MongoDB supports pagination queries XiaoMi#851 (X…
Browse files Browse the repository at this point in the history
  • Loading branch information
goodjava authored Jul 2, 2024
1 parent 048dcc6 commit 7ab5014
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 17 deletions.
7 changes: 7 additions & 0 deletions jcommon/ai/bytedance/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@
<artifactId>jackson-databind</artifactId>
<version>2.12.4</version>
</dependency>

<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.10.0</version>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package run.mone.ai.bytedance;


import okhttp3.*;

/**
* @author [email protected]
* @date 2024/5/18 17:20
*/
public class DoubaoClient {

private static String ARK_API_KEY = "";

public static void main(String[] args) {
OkHttpClient client = new OkHttpClient();
String json = "{\n" +
" \"model\": \"\",\n" +
" \"messages\": [\n" +
" {\n" +
" \"role\": \"system\",\n" +
" \"content\": \"You are a helpful assistant.\"\n" +
" },\n" +
" {\n" +
" \"role\": \"user\",\n" +
" \"content\": \"Hello!\"\n" +
" }\n" +
" ],\n" +
" \"stream\": false\n" +
"}";

MediaType JSON = MediaType.get("application/json; charset=utf-8");
RequestBody body = RequestBody.create(json, JSON);

Request request = new Request.Builder()
.url("")
.addHeader("Content-Type", "application/json")
.addHeader("Authorization", "Bearer " + ARK_API_KEY) // 确保你已经设置了 ARK_API_KEY 变量
.post(body)
.build();

try (Response response = client.newCall(request).execute()) {
// 处理响应
if (response.isSuccessful()) {
String responseBody = response.body().string();
System.out.println(responseBody);
} else {
System.out.println(response);
}
} catch (Exception e) {
e.printStackTrace();
}


}

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ public void before(ProceedingJoinPoint point) {
}
}

//没有就当普通用户处理
if (null == user.getRole()) {
user.setRole("user");
}

//必须有后台管理权限
if (role.equals("admin")) {
if (null == user || !user.getRole().equals("admin")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Map;

/**
* 用户
*
Expand All @@ -17,7 +19,7 @@
@AllArgsConstructor
@NoArgsConstructor
@Entity("user")
public class User implements MongoBo{
public class User implements MongoBo {

@Id
private String id;
Expand Down Expand Up @@ -57,6 +59,8 @@ public class User implements MongoBo{

private String token;

private Map<String, String> meta;

public User(String username, String password) {
this.username = username;
this.password = password;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.xiaomi.youpin.docean.anno.Service;
import dev.morphia.Datastore;
import dev.morphia.UpdateOptions;
import dev.morphia.query.FindOptions;
import dev.morphia.query.filters.Filter;
import dev.morphia.query.filters.Filters;
import dev.morphia.query.updates.UpdateOperator;
Expand Down Expand Up @@ -65,6 +66,27 @@ public List<T> findAll(Filter filter) {
return datastore.find(this.clazz).filter(filter).iterator().toList();
}

public List<T> findAll() {
return datastore.find(this.clazz).iterator().toList();
}

//实现分页查询(class)
public List<T> findWithPagination(Filter filter, int page, int size) {
FindOptions options = new FindOptions()
.skip((page - 1) * size)
.limit(size);
return datastore.find(this.clazz)
.filter(filter)
.iterator(options)
.toList();
}

//查询总页数(class)
public long getTotalPages(Filter filter, int pageSize) {
long totalRecords = datastore.find(this.clazz).filter(filter).count();
return (totalRecords + pageSize - 1) / pageSize;
}

public boolean delete(T t) {
datastore.delete(t);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ public void testWebSocketServer() {
@Test
public void testWebSocketClient() {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url("ws://127.0.0.1:8899/ws").build();
// Request request = new Request.Builder().url("ws://127.0.0.1:8899/ws").build();
Request request = new Request.Builder().url("").build();
WebSocketListener listener = new WebSocketListener() {
@Override
public void onOpen(WebSocket webSocket, Response response) {
Expand Down
30 changes: 15 additions & 15 deletions jcommon/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -232,21 +232,21 @@
</configuration>
</plugin>

<!-- <plugin>-->
<!-- <artifactId>maven-source-plugin</artifactId>-->
<!-- <version>2.1</version>-->
<!-- <executions>-->
<!-- <execution>-->
<!-- <phase>compile</phase>-->
<!-- <goals>-->
<!-- <goal>jar</goal>-->
<!-- </goals>-->
<!-- </execution>-->
<!-- </executions>-->
<!-- <configuration>-->
<!-- <attach>true</attach>-->
<!-- </configuration>-->
<!-- </plugin>-->
<plugin>
<artifactId>maven-source-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
<configuration>
<attach>true</attach>
</configuration>
</plugin>

<!-- <plugin>-->
<!-- <groupId>org.apache.maven.plugins</groupId>-->
Expand Down

0 comments on commit 7ab5014

Please sign in to comment.