Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[enhancement] JCommon MongoDB supports pagination queries #851 #852

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading