Java tutorial
/* * 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 com.ar.dev.tierra.api.controller; import com.ar.dev.tierra.api.model.Factura; import com.ar.dev.tierra.api.model.JsonResponse; import com.ar.dev.tierra.api.model.Usuarios; import com.ar.dev.tierra.api.resource.FacadeService; import java.io.Serializable; import java.math.BigDecimal; import java.util.Date; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.oauth2.provider.OAuth2Authentication; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; /** * * @author PauloGaldo */ @RestController @RequestMapping("reserva") public class ReservaController implements Serializable { @Autowired FacadeService facadeService; @RequestMapping(value = "/add", method = RequestMethod.POST) public ResponseEntity<?> add(OAuth2Authentication authentication, @RequestBody Factura factura) { Usuarios user = facadeService.getUsuariosDAO().findUsuarioByUsername(authentication.getName()); factura.setUsuarioCreacion(user.getIdUsuario()); factura.setFechaCreacion(new Date()); factura.setEstado("RESERVADO"); factura.setIdSucursal(user.getUsuarioSucursal().getIdSucursal()); factura.setTotal(BigDecimal.ZERO); int idFactura = facadeService.getFacturaDAO().add(factura); JsonResponse msg = new JsonResponse("Success", String.valueOf(idFactura)); return new ResponseEntity<>(msg, HttpStatus.OK); } @RequestMapping(value = "/update", method = RequestMethod.POST) public ResponseEntity<?> update(OAuth2Authentication authentication, @RequestBody Factura factura) { Usuarios user = facadeService.getUsuariosDAO().findUsuarioByUsername(authentication.getName()); factura.setUsuarioModificacion(user.getIdUsuario()); factura.setFechaModificacion(new Date()); facadeService.getFacturaDAO().update(factura); JsonResponse msg = new JsonResponse("Success", "Reserva modificada con exito"); return new ResponseEntity<>(msg, HttpStatus.OK); } @RequestMapping(value = "/day", method = RequestMethod.GET) public ResponseEntity<?> getDay() { List<Factura> list = facadeService.getFacturaDAO().getDiaryReserva(); if (!list.isEmpty()) { return new ResponseEntity<>(list, HttpStatus.OK); } else { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } } @RequestMapping(value = "/day/paged", method = RequestMethod.GET) public ResponseEntity<?> getDayPaged(OAuth2Authentication authentication, @RequestParam(value = "page", required = false, defaultValue = "0") Integer page, @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) { Usuarios user = facadeService.getUsuariosDAO().findUsuarioByUsername(authentication.getName()); Page factura = facadeService.getFacturaService().getReservasDay(page, size, user.getUsuarioSucursal().getIdSucursal()); if (factura != null) { return new ResponseEntity<>(factura, HttpStatus.OK); } else { return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } } @RequestMapping(value = "/month", method = RequestMethod.GET) public ResponseEntity<?> getMonth() { List<Factura> list = facadeService.getFacturaDAO().getMonthReserva(); if (!list.isEmpty()) { return new ResponseEntity<>(list, HttpStatus.OK); } else { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } } @RequestMapping(value = "/month/paged", method = RequestMethod.GET) public ResponseEntity<?> getMonthPaged(OAuth2Authentication authentication, @RequestParam(value = "page", required = false, defaultValue = "0") Integer page, @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) { Usuarios user = facadeService.getUsuariosDAO().findUsuarioByUsername(authentication.getName()); Page factura = facadeService.getFacturaService().getReservasMonth(page, size, user.getUsuarioSucursal().getIdSucursal()); if (factura != null) { return new ResponseEntity<>(factura, HttpStatus.OK); } else { return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } } }