今天我们实作Users的CRUD,但今天因为花很多时间在前端的Header的排版,所以没有什麽时间可以细作Users,
我们先根据Native Camp的会员页和注册页来看大概需要什麽栏位後暂时先宣告好我们的schema。
Users.java 题外话lombok很好用,省了很多工
package com.mock.nativecamp.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection = "Users")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Users {
@Id
private String id;
private String name;
private String email;
private String password;
private String status;
private String coin;
private String timezone;
private String payMethod;
private String nextPayCheck;
private String ssoId;
}
然後就可以开始撰写我们的controller和service了,controller主要是定义API接口,service则是做逻辑处理和Repository CRUD的呼叫。
由於时间关系我只做到Create User和Get all Users和Get one User。
UsersController.java
package com.mock.nativecamp.controller;
import com.mock.nativecamp.model.Users;
import com.mock.nativecamp.service.UsersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
@Slf4j
@RestController
@RequestMapping("/users")
public class UsersController {
private final UsersService usersService;
public UsersController(UsersService usersService) {
this.usersService = usersService;
}
/**
* Signup user
* @param user
* @return AdminUser
* @throws Exception
*/
@PostMapping(path = "/signup", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public Object SignupUser(@RequestBody Users user) throws Exception {
return usersService.signupUser(user);
}
/**
* Get all Users
* @return Users list
* @throws Exception
*/
@GetMapping(path = "/all", produces = MediaType.APPLICATION_JSON_VALUE)
public Object GetAllUsers() throws Exception {
return usersService.getAllUsers();
}
/**
* Get one user
* @return Users
* @throws Exception
*/
@GetMapping(path = "/user", produces = MediaType.APPLICATION_JSON_VALUE)
public Object GetUsers(@RequestParam String id) throws Exception {
return usersService.getUser(id);
}
}
因为只是简单的测试是否可以操作存取MongoDB,尚未做逻辑检查。
UsersService.java
package com.mock.nativecamp.service;
import com.mock.nativecamp.model.Users;
import com.mock.nativecamp.repository.UsersRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class UsersService {
@Autowired
private UsersRepository usersRepository;
public Object signupUser(Users user) {
usersRepository.save(user);
return new ResponseEntity(HttpStatus.ACCEPTED);
}
public Object getAllUsers() {
return usersRepository.findAll();
}
public Object getUser(String id) {
return usersRepository.findById(id);
}
}
然後运行起来使用postman来测试
新增使用者
取得使用者列表
取得单一使用者
下一篇应该是会把CRUD都给完成,然後透过网站end to end的确认逻辑後再来实作在service上。
Filebeat 本篇介绍filebeat是什麽以及如何安装及建置 下一篇浏览此篇建置完成的kiba...
为什麽要把资料建成Excel ? 由於这次资料量比Icebear在学习的时候多很多,所以决定先把资料...
今天的内容会跟各位介绍 Google Cloud 相关的基础知识,希望不会不小心的讲成像业配文QQ,...
ScriptTagHelper (脚本标签帮助程序):是针对HTML原生<script> tag的...
第二十八天了,讲到练习题第七题 不知不觉已经快结束了,因为篇幅的关系,所以可能无法说完全部的练习题 ...