Newer
Older
JerseyDemo / src / main / java / com / example / springtest / demo / resources / UserRest.java
package com.example.springtest.demo.resources;

import com.example.springtest.demo.entities.User;
import com.example.springtest.demo.repositories.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.*;

@Component
@Path("/users")
public class UserRest {

//    private Map<String, User> users = new HashMap<>();

    @Autowired // This means to get the bean called userRepository
    // Which is auto-generated by Spring, we will use it to handle the data
    private UserRepository userRepository;
//    @GET
//    public String getUser(@QueryParam("uId") String uId, @QueryParam("name") String name, @QueryParam("password") String password) {
//        ObjectMapper objectMapper = new ObjectMapper();
//        String json = null;
//        try {
//            json = objectMapper.writeValueAsString(new User(uId, name, password));
//        } catch (JsonProcessingException e) {
//            e.printStackTrace();
//        }
//        return json;
//    }
    @GET
    @Produces(MediaType.APPLICATION_JSON) // 戻り値をJSONで返す.
//    public Collection<User> getUsers() {
    public Iterable<User> getUsers() {
//        return users.values();    // JSONが返る
        return userRepository.findAll();
    }

    @Path("/{uId}")
    @GET
    @Produces(MediaType.APPLICATION_JSON) // 戻り値をJSONで返す.
//    public User getUser(@PathParam("uId") String uId) {
    public Optional<User> getUser(@PathParam("uId") String uId) {
        Optional<User> user = userRepository.findById(uId);
//        User user = users.get(uId);
        if (user.isPresent()) {
            return user;    // JSONが返る
        } else { // uIdが無ければ
            throw new WebApplicationException(404);    // 404が返る
        }
    }

    @POST
    @Produces(MediaType.APPLICATION_JSON)
    public User createUser(@FormParam("name") String name, @FormParam("password") String password) {
        String uId = UUID.randomUUID().toString();
        User user = new User(uId, name, password);
//        users.put(uId, user);
        return userRepository.save(user);
    }

    @Path("/{uId}")
    @PUT
    @Produces(MediaType.APPLICATION_JSON)
    public User updateUser(@PathParam("uId") String uId, @Nullable @FormParam("name") String name, @Nullable @FormParam("password") String password) {
//        User user = users.get(uId);
        Optional<User> user = userRepository.findById(uId);
        if (user.isPresent()) {
            user.get().setName(name);
            user.get().setPassword(password);
            return userRepository.save(user.get());    // JSONが返る
        } else { // uIdが無ければ
            throw new WebApplicationException(404);    // 404が返る
        }
    }

    @Path("/{uId}")
    @DELETE
    @Produces(MediaType.APPLICATION_JSON)
    public User deleteUser(@PathParam("uId") String uId) {
        Optional<User> user = userRepository.findById(uId);
        if (user.isPresent()) {
            userRepository.deleteById(uId);
            return user.get();   // JSONが返る
        } else { // uIdが無ければ
            throw new WebApplicationException(410);    // 410が返る(GONE)
        }
    }
}