-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Otavio Santana <[email protected]>
- Loading branch information
1 parent
53eae13
commit ae7ad79
Showing
2 changed files
with
74 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,27 @@ | ||
package org.soujava.samples.hotel; | ||
|
||
import jakarta.data.page.Page; | ||
import jakarta.data.page.PageRequest; | ||
import jakarta.data.repository.By; | ||
import jakarta.data.repository.Delete; | ||
import jakarta.data.repository.Find; | ||
import jakarta.data.repository.Repository; | ||
import jakarta.data.repository.Save; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@Repository | ||
public interface Hotel { | ||
|
||
@Save | ||
Guest checkIn(Guest guest); | ||
Room checkIn(Room room); | ||
|
||
@Delete | ||
void checkOut(Guest guest); | ||
void checkOut(Room room); | ||
|
||
@Find | ||
Optional<Guest> reservation(@By(org.soujava.samples.hotel._Room.NUMBER) int number); | ||
Optional<Room> reservation(@By(org.soujava.samples.hotel._Room.NUMBER) int number); | ||
|
||
Page<Room> findBy(PageRequest pageRequest); | ||
} |
66 changes: 66 additions & 0 deletions
66
src/main/java/org/soujava/samples/hotel/HotelResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package org.soujava.samples.hotel; | ||
|
||
import jakarta.data.page.PageRequest; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.ws.rs.DELETE; | ||
import jakarta.ws.rs.DefaultValue; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.PUT; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.PathParam; | ||
import jakarta.ws.rs.QueryParam; | ||
import jakarta.ws.rs.WebApplicationException; | ||
import jakarta.ws.rs.core.Response; | ||
|
||
import java.util.List; | ||
import java.util.logging.Logger; | ||
|
||
@Path("/hotels") | ||
@ApplicationScoped | ||
public class HotelResource { | ||
|
||
private static final Logger LOGGER = Logger.getLogger(HotelResource.class.getName()); | ||
|
||
private final Hotel hotel; | ||
|
||
public HotelResource(Hotel hotel) { | ||
this.hotel = hotel; | ||
} | ||
|
||
public HotelResource() { | ||
this(null); | ||
} | ||
|
||
@GET | ||
public List<Room> rooms(@QueryParam("page") @DefaultValue("1") int page, | ||
@QueryParam("size") @DefaultValue("1") int size) { | ||
LOGGER.info("Finding rooms page: " + page + " size: " + size); | ||
var pageRequest = PageRequest.ofPage(page).size(size); | ||
var rooms = hotel.findBy(pageRequest).content(); | ||
LOGGER.info("Found rooms: " + rooms.size()); | ||
return rooms; | ||
} | ||
|
||
@GET | ||
@Path("/{number}") | ||
public Room reservation(@PathParam("number") int number) { | ||
LOGGER.info("Finding reservation: " + number); | ||
return hotel.reservation(number) | ||
.orElseThrow(() -> new WebApplicationException("Room not found", Response.Status.NOT_FOUND)); | ||
} | ||
|
||
@PUT | ||
public Room checkIn(Room room) { | ||
LOGGER.info("Check in: " + room); | ||
return hotel.checkIn(room); | ||
} | ||
|
||
@DELETE | ||
@Path("/{number}") | ||
public void checkOut(@PathParam("number") int number) { | ||
LOGGER.info("Check out: " + number); | ||
var room = hotel.reservation(number) | ||
.orElseThrow(() -> new WebApplicationException("Room not found", Response.Status.NOT_FOUND)) | ||
hotel.checkOut(room); | ||
} | ||
} |