我正在將 Spring Boot 與 MongoDB 一起使用。我是 MongoDb 的初學者。
我試圖實作的是,當我得到我的端點的呼叫,或在請求我的終點,我想,這將有狀態的資料庫中創建一個表REQUESTED,應用userID,然后領域url,version,requestedDate(居住在創建表)completedDate(當 URL 保存到資料庫時填充)。
之后,我執行所有我必須執行的邏輯,在我的情況下,邏輯是我應該檢索用戶的 URL 并將其保存到發出 URL 請求時創建的同一個表中。
后URL會保存在請求時,我應該已經創建了同桌userId,url,version,requestedDate,completedDate和狀態COMPLETED。
我到目前為止的代碼是:
用戶地址.java
public class UserUrl{
public final MongoUserId mongoUserId;
public final Optional<String> url;
public final long version;
public final Instant requestedDate;
public final Instant completeDate;
public final State state;
public UserUrl(MongoUserId mongoUserId,
Optional<String> url,
long version,
Instant requestedDate,
State state) {
this.mongoUserId = mongoUserId;
this.url = url;
this.version = version;
this.requestedDate = requestedDate;
this.state = state;
}
public static UserUrl create(MongoUserId mongoUserId){
return new UserUrl(
mongoUserId,
Optional.empty(),
0,
Instant.now(),
State.REQUESTED
);
}
public UserUrl withUrl(String url){
return new UserUrl(
this.mongoUserId,
Optional.of(url),
0,
Instant.now(),
State.COMPLETED
);
}
}
用戶控制器.java
@RequestMapping(value = "/trigger/url/{userId}",
method = RequestMethod.GET)
public void getUrlForUser(@PathVariable("userId") String userId) {
userUrlRepository.save(UserUrl.create(MongoUserId.of(userId)));
URL getUrl = userDataService.getUrlUser(userId);
String url = getUrl.toString();
if (url != null) {
//here I should probably do something to set withUrl method in model
return new ResponseEntity<>(url, HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
我不確定我應該如何填充UserUrl模型URL?
另外,如果我放入completedDate建構式,那么我想在創建時也使用它,但是我應該對其應用什么值?
任何建議表示贊賞:)
uj5u.com熱心網友回復:
您應該使用使用 持久化的物體CrudRepository,更新它并在需要時再次保存它:
@RequestMapping(value = "/trigger/url/{userId}", method = RequestMethod.GET)
public void getUrlForUser(@PathVariable("userId") String userId) {
UserUrl userUrl = userUrlRepository.save(UserUrl.create(MongoUserId.of(userId)));
URL getUrl = userDataService.getUrlUser(userId);
String url = getUrl.toString();
if (url != null) {
userUrlRepository.save(userUrl.withUrl(url));
return new ResponseEntity<>(url, HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
編輯
要completeDate在完成時更改,即url屬性已決議后,請更新withUrl構建器方法以傳播this.requestedDate并將 初始化completeDate為Instant.now():
public class UserUrl {
public final MongoUserId mongoUserId;
public final Optional<String> url;
public final long version;
public final Instant requestedDate;
public final Instant completeDate;
public final State state;
public UserUrl(MongoUserId mongoUserId,
Optional<String> url,
long version,
Instant requestedDate,
Instant completeDate,
State state) {
this.mongoUserId = mongoUserId;
this.url = url;
this.version = version;
this.requestedDate = requestedDate;
this.completeDate = completeDate;
this.state = state;
}
public static UserUrl create(MongoUserId mongoUserId){
return new UserUrl(
mongoUserId,
Optional.empty(),
0,
Instant.now(),
null,
State.REQUESTED
);
}
public UserUrl withUrl(String url){
return new UserUrl(
this.mongoUserId,
Optional.of(url),
0,
this.requestedDate,
Instant.now(),
State.COMPLETED
);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/350909.html
