Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
185 changes: 185 additions & 0 deletions src/main/java/reserve/controller/BookingController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
package reserve.controller;


import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import reserve.model.Booking;
import reserve.model.Product;
import reserve.model.User;
import reserve.repository.BookingRepository;
import reserve.repository.ProductRepository;
import reserve.repository.UserRepository;

@CrossOrigin(origins = "*")
@RestController
public class BookingController {
@Autowired
BookingRepository bookingRepository;

@Autowired
UserRepository userRepository;

@Autowired
ProductRepository productRepository;

@PostMapping("/book")
public ResponseEntity<Object> createBooking(@RequestBody Booking body) {
try {
User user = userRepository.findById(body.getUser().getUserID()).orElse(null);
Product product = productRepository.findById(body.getProduct().getProductid()).orElse(null);

if (user != null && product != null) {
body.setUser(user);
body.setProduct(product);

int total = body.getQuantity() * product.getPrice();
body.setTotal(total);

Booking newBooking = bookingRepository.save(body);

return new ResponseEntity<>(newBooking, HttpStatus.CREATED);
} else {
return new ResponseEntity<>("User or Product not found.", HttpStatus.BAD_REQUEST);
}
} catch (Exception e) {
System.out.println(e.getMessage());
return new ResponseEntity<>("Internal server error.", HttpStatus.INTERNAL_SERVER_ERROR);
}
}


@GetMapping("/book")
public ResponseEntity<Object> getBooking() {
try {

List<Booking> booking = bookingRepository.findAll();
return new ResponseEntity<>(booking, HttpStatus.OK);

} catch (Exception e) {
System.out.println(e.getMessage());
return new ResponseEntity<>("Integer server error", HttpStatus.INTERNAL_SERVER_ERROR);
}
}

@GetMapping("/book/stale/{userID}")
public ResponseEntity<Object> getBookingStale(@PathVariable Long userID) {
try {
List<Booking> bookings = bookingRepository.findByUserUserIDAndStatus(userID, "1");
return new ResponseEntity<>(bookings, HttpStatus.OK);
} catch (Exception e) {
System.out.println(e.getMessage());
return new ResponseEntity<>("Internal server error", HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@GetMapping("/book/paid/{userID}")
public ResponseEntity<Object> getBooking(@PathVariable Long userID) {
try {
List<Booking> bookings = bookingRepository.findByUserUserIDAndStatus(userID, "0");
return new ResponseEntity<>(bookings, HttpStatus.OK);
} catch (Exception e) {
System.out.println(e.getMessage());
return new ResponseEntity<>("Internal server error", HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@GetMapping("/book/{reviewID}")
public ResponseEntity<Object> getBooking(@PathVariable List<Long> reviewID) {
try {
List<Booking> bookings = bookingRepository.findByReviewIDIn(reviewID);
return new ResponseEntity<>(bookings, HttpStatus.OK);
} catch (Exception e) {
System.out.println(e.getMessage());
return new ResponseEntity<>("Internal server error", HttpStatus.INTERNAL_SERVER_ERROR);
}
}

@PutMapping("/book/{userID}")
public ResponseEntity<Object> updateBookingStatus(
@PathVariable Long userID,
@RequestBody List<Booking> request) {
try {
List<Booking> bookings = bookingRepository.findByUserUserID(userID);

for (Booking update : request) {
for (Booking booking : bookings) {
if (booking.getReviewID().equals(update.getReviewID())) {
Timestamp currentTimestamp = new Timestamp(System.currentTimeMillis());
booking.setDate_book(currentTimestamp);
booking.setStatus("0");
}
}
}

bookingRepository.saveAll(bookings);

return new ResponseEntity<>("Status updated to '0' successfully", HttpStatus.OK);
} catch (Exception e) {
System.out.println(e.getMessage());
return new ResponseEntity<>("Internal server error", HttpStatus.INTERNAL_SERVER_ERROR);
}
}

@PutMapping("/book/day/{reviewID}")
public ResponseEntity<Object> updateBookingDate(
@PathVariable Long reviewID,
@RequestBody Booking request) {
try {
Optional<Booking> bookingOptional = bookingRepository.findById(reviewID);

if (bookingOptional.isPresent()) {
Booking booking = bookingOptional.get();

booking.setDate_reciept(request.getDate_reciept());

bookingRepository.save(booking);

return new ResponseEntity<>("date_receipt updated successfully", HttpStatus.OK);
} else {
return new ResponseEntity<>("Booking not found", HttpStatus.NOT_FOUND);
}
} catch (Exception e) {
System.out.println(e.getMessage());
return new ResponseEntity<>("Internal server error", HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@DeleteMapping("/book/{reviewID}")
public ResponseEntity<Object> deleteBookById(@PathVariable("reviewID") Long reviewID) {

try {

Optional<Booking> bookingFind = bookingRepository.findById(reviewID);

if (bookingFind.isPresent()) {


bookingRepository.delete(bookingFind.get());

return new ResponseEntity<>("Delete Product Success.", HttpStatus.OK);

} else {
return new ResponseEntity<>("Product Not Found.", HttpStatus.BAD_REQUEST);
}

} catch (Exception e) {
System.out.println(e.getMessage());
return new ResponseEntity<>("Internal server error.", HttpStatus.INTERNAL_SERVER_ERROR);
}

}

}
83 changes: 69 additions & 14 deletions src/main/java/reserve/controller/ProductController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,40 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


import com.fasterxml.jackson.databind.ObjectMapper;

import reserve.model.Product;
import reserve.model.User;
import reserve.repository.ProductRepository;


@CrossOrigin(origins = "*")
@RestController
public class ProductController {

@Autowired
ProductRepository productRepository;


@PostMapping(value = "/Productphoto", consumes = { "multipart/form-data" })
public ResponseEntity<Object> createPhoto(@RequestParam("body") String Productjson,
@RequestParam("photo") MultipartFile photo) throws IOException {
Expand All @@ -53,9 +58,8 @@ public ResponseEntity<Object> createPhoto(@RequestParam("body") String Productjs
return new ResponseEntity<>("Internal server error.", HttpStatus.INTERNAL_SERVER_ERROR);
}
}

@GetMapping("/Product")
public ResponseEntity<Object> getBoardGames() {
public ResponseEntity<Object> getProduct() {

try {
List<Product> ProductList = productRepository.findAllProduct();
Expand All @@ -64,15 +68,16 @@ public ResponseEntity<Object> getBoardGames() {
for (Product row : ProductList) {
Long productid = row.getProductid();
String productname = row.getProductname();
String price = row.getPrice();
Integer price = row.getPrice();
Integer piece = row.getPiece();
byte[] photoData = row.getPhotoData();
String detail = row.getDetail();

User user = row.getUser();



Product newProduct = new Product(productid, productname, price, photoData, detail, user);
Product newProduct = new Product(productid, productname, price, piece, photoData, detail, user);

Product.add(newProduct);
}
Expand All @@ -85,7 +90,7 @@ public ResponseEntity<Object> getBoardGames() {
}

@GetMapping("/Product/{productid}")
public ResponseEntity<Object> getBoardGameById(@PathVariable("productid") Long productid) {
public ResponseEntity<Object> getProductById(@PathVariable("productid") Long productid) {

try {

Expand Down Expand Up @@ -124,7 +129,7 @@ public ResponseEntity<Object> deleteProductById(@PathVariable("productid") Long
return new ResponseEntity<>("Delete Product Success.", HttpStatus.OK);

} else {
return new ResponseEntity<>("Board Product Found.", HttpStatus.BAD_REQUEST);
return new ResponseEntity<>("Product Not Found.", HttpStatus.BAD_REQUEST);
}

} catch (Exception e) {
Expand All @@ -134,7 +139,7 @@ public ResponseEntity<Object> deleteProductById(@PathVariable("productid") Long

}

@PutMapping(value = "/Product/{productid}", consumes = { "multipart/form-data" })
@PutMapping(value = "/Product/all/{productid}", consumes = { "multipart/form-data" })
public ResponseEntity<Object> updateProduct(@PathVariable("productid") Long productid,
@RequestParam("body") String Productjson, @RequestParam("photo") MultipartFile photo) throws IOException {

Expand Down Expand Up @@ -170,9 +175,59 @@ public ResponseEntity<Object> updateProduct(@PathVariable("productid") Long prod
}

}




@PutMapping(value = "/Product/Piece/{productid}", consumes = { "application/json" })
public ResponseEntity<Object> updatePiece(
@PathVariable("productid") Long productid,
@RequestBody Map<String, Integer> requestBody
) {
try {
Optional<Product> productFind = productRepository.findById(productid);

if (productFind.isPresent()) {
Product productUpdate = productFind.get();
int change = requestBody.get("change");
int newPiece = productUpdate.getPiece() - change;

if (newPiece >= 0) {
productUpdate.setPiece(newPiece);
productRepository.save(productUpdate);
return new ResponseEntity<>(productUpdate, HttpStatus.OK);
} else {
return new ResponseEntity<>("Invalid quantity change.", HttpStatus.BAD_REQUEST);
}
} else {
return new ResponseEntity<>("Product Not Found.", HttpStatus.BAD_REQUEST);
}
} catch (Exception e) {
System.out.println(e.getMessage());
return new ResponseEntity<>("Internal server error.", HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@PutMapping("/Product/{productid}")
public ResponseEntity<Object> updateProduct(@PathVariable("productid") Long productid, @RequestBody Product body) {
try {
Optional<Product> ProductFound = productRepository.findById(productid);

if (ProductFound.isPresent()) {
Product productToUpdate = ProductFound.get();

productToUpdate.setProductname(body.getProductname());
productToUpdate.setPrice(body.getPrice());
productToUpdate.setPiece(body.getPiece());
productToUpdate.setDetail(body.getDetail());


productRepository.save(productToUpdate);

return new ResponseEntity<>(productToUpdate, HttpStatus.OK);
} else {
return new ResponseEntity<>("Product Not Found.", HttpStatus.BAD_REQUEST);
}
} catch (Exception e) {
System.out.println(e.getMessage());
return new ResponseEntity<>("Internal server error.", HttpStatus.INTERNAL_SERVER_ERROR);
}
}

}
Loading