chalk.web.ChalkController.java Source code

Java tutorial

Introduction

Here is the source code for chalk.web.ChalkController.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package chalk.web;

import chalk.service.RegistrationMgr;
import chalk.domain.*;
import chalk.service.MessageMgr;
import com.sun.xml.internal.ws.client.ResponseContext;
import java.util.List;
import javax.persistence.*;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 *
 * @author Joris
 */
@RestController
public class ChalkController {

    private RegistrationMgr registrationMgr;
    private MessageMgr messageMgr;
    private EntityManagerFactory emf;
    private EntityManager em;

    public ChalkController() {
        this.emf = Persistence.createEntityManagerFactory("ChalkWebservicePU");
        this.em = emf.createEntityManager();
        registrationMgr = new RegistrationMgr(em);
        messageMgr = new MessageMgr(em);
    }

    @RequestMapping(value = "/user/register", method = RequestMethod.POST)
    public @ResponseBody User registerUser(@RequestBody User user) {
        System.out.println("Executing: registerUser()");
        return registrationMgr.registerUser(user);
    }

    @RequestMapping(value = "/user/get/{phone}", method = RequestMethod.GET)
    public @ResponseBody User getUser(@PathVariable("phone") String phoneNumber) {
        System.out.println("Executing: getUser()");
        return registrationMgr.getUser(phoneNumber);
    }

    @RequestMapping(value = "/message/create", method = RequestMethod.POST)
    public @ResponseBody Message createMessage(@RequestBody Message message) {
        System.out.println("Executing: createMessage()");
        return messageMgr.createMessage(message);
    }

    @RequestMapping(value = "/message/find", method = RequestMethod.GET)
    public @ResponseBody ResponseEntity<List<Message>> findAllFor(
            @RequestParam(value = "sender") String senderPhone,
            @RequestParam(value = "receiver") String receiverPhone) {
        System.out.println("Executing: findAllFor()");
        List<Message> result = messageMgr.findAllFor(senderPhone, receiverPhone);
        if (result == null) {
            return new ResponseEntity(HttpStatus.BAD_REQUEST);
        }
        return new ResponseEntity(result, HttpStatus.OK);
    }
}