【发布时间】:2020-10-10 01:23:11
【问题描述】:
我正在设计一个带有 spring 后端的移动应用程序。
在我的移动应用程序中,我有一个随机播放按钮。当用户单击按钮时,我会从 GPS 获取用户的当前位置并发送请求,然后使用此位置信息查找当前用户附近的用户。
顺便说一下,使用相同的用例对用例进行随机播放和过滤,唯一的区别是当随机播放我应用默认过滤器时,比如所有年龄、所有距离等。
~~ 随机/过滤请求 ~~
{
interestedGenders: ["WOMAN"]
minDistance: 1,
maxDistance: 100,
minAge: 18,
maxAge: 65,
latitude: 31.4,
longitude: 27.1
}
好的,现在我的问题是我不想实时跟踪/更新用户位置,所以我想当用户随机播放(或过滤)时我已经有了用户位置,所以首先我更新数据库中的用户位置然后我使用这个位置信息使用其他过滤器(性别、年龄等)查找附近的用户
我在 mongo db 中有 3 个简单的集合(帐户、配置文件和 userLocations)
~~个人资料文件(简体)~~
{
id: "xxx",
accountId: "yyy",
gender: "MAN",
interestedGenders: ["WOMAN"]
}
~~用户位置文件~~
{
id: "zzz"
accountId: "yyy",
location: { type: "Point", coordinates: [31.4, 27.1] }
lastUpdated: "2020-10-10T00:59:37.154Z"
}
这是我使用的代码。首先我执行更新用户位置用例。如果用户在数据库中没有记录,则更新位置用例创建记录,否则更新用户位置。然后我发现用户符合条件。
@ApiController
@AllArgsConstructor
public class FilterProfilesController {
private final UpdateUserLocationUseCase updateUserLocationUseCase;
private final FilterProfilesUseCase filterProfilesUseCase;
@PostMapping("/profiles/filter")
public ResponseEntity<BaseResponse> filterProfiles(@AuthenticationPrincipal AccountId accountId, @RequestBody FilterProfilesRequest request) {
var userLocation = new Location(new Latitude(request.getLatitude()), new Longitude(request.getLongitude()));
updateUserLocationUseCase.execute(new UpdateUserLocationUseCase.Command(accountId, userLocation), () -> {
});
// Conversions from request to value objects (simplified)
var query = new FilterProfilesUseCase.Query(accountId, userLocation, interestedGenders, ageRange, distanceRange);
var presenter = new FilterProfilesPresenter();
filterProfilesUseCase.execute(query, presenter);
return presenter.getViewModel();
}
}
设计这个的最佳方法是什么?
如何解耦更新位置用例(来自 FilterProfilesController)但在过滤用例之前仍然调用?
我应该写一个自定义方面还是什么?
一些问题的答案
问:为什么我没有将用户位置信息放入个人资料集合中?
A:因为我们在用户注册时没有得到这个信息。所以位置字段保持为空,直到用户使用随机播放或过滤页面并且我在 mongo db 中使用地理空间查询,所以我猜这可能会导致错误。
加上配置文件对象已经有很多字段,我认为分离用户位置为未来的用例提供了很大的灵活性。
【问题讨论】:
标签: spring spring-boot architecture domain-driven-design clean-architecture