Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Spring
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Liutauras Vilda
Paul Clapham
Sheriffs:
paul wheaton
Tim Cooke
Henry Wong
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Piet Souris
Bartenders:
Mike London
Forum:
Spring
I need help writing an update controller and delete controller
martin codey
Ranch Hand
Posts: 90
1
posted 2 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
//Controller class
package com.newton.holidaymaker.controllers; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; 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.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.newton.holidaymaker.models.Booking; import com.newton.holidaymaker.models.Room; import com.newton.holidaymaker.repositories.RoomRepository; import com.newton.holidaymaker.services.RoomService; @RestController @RequestMapping("/") public class RoomController { @Autowired private RoomService roomService; @RequestMapping(method = RequestMethod.GET, value = "/all") public List<Room> findAll(){ return roomService.findAll(); } @RequestMapping(method = RequestMethod.GET, value = "/get-room/{id}") public Room getRoomById(@PathVariable Integer id){ Room room = roomService.getRoomById(id); if (room != null){ return roomService.getRoomById(id); } return null; } @RequestMapping(method = RequestMethod.POST, value = "/add-room") public Room addRoom(@RequestBody Room room){ return roomService.addRoom(room); } }
//Service class
package com.newton.holidaymaker.services; import java.util.ArrayList; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.newton.backend.model.Person; import com.newton.holidaymaker.models.Room; import com.newton.holidaymaker.repositories.RoomRepository; import springboot.starter.course.Course; import springboot.starter.topic.Topic; @Service public class RoomService { @Autowired private RoomRepository roomRepository; public List<Room> findAll(){ List<Room> room = new ArrayList<>(); roomRepository.findAll().forEach(room::add); return room; } public Room getRoomById(Integer id) { if(roomRepository.existsById(id)){ return roomRepository.findById(id).get(); } return null; } public Room addRoom(Room room){ return roomRepository.save(room); } public Room updateRoom(Room room) { return roomRepository.save(room); } public void deleteRoom(Integer id) { roomRepository.deleteById(id); } }
//entity
package com.newton.holidaymaker.models; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="room") public class Room { @Id @GeneratedValue (strategy = GenerationType.IDENTITY) private int id; /* @Column(name="roomId") private int roomId;*/ @Column(name="roomType") private String roomType; @Column(name="roomPrice") private double roomPrice; @Column(name="hotelId") private int hotelId; /*@Column(name="bookingId") private int bookingId;*/ @Column(name="isBooked") private Boolean isBooked; public Room() { } public Room(Integer id,String roomType, double roomPrice, int hotelId, /*int bookingId, */ boolean isBooked) { this.id = id; this.roomType = roomType; this.roomPrice = roomPrice; this.hotelId = hotelId; //this.bookingId = bookingId; this.isBooked = isBooked; } public void copy(Room r) { this.id = r.id; this.roomType = r.roomType; this.roomPrice = r.roomPrice; this.hotelId = r.hotelId; //this.bookingId = r.bookingId; this.isBooked = r.isBooked; } /* public int getRoomId() { return roomId; } public void setRoomId(int roomId) { this.roomId = roomId; } */ public String getRoomType() { return roomType; } public void setRoomType(String roomType) { this.roomType = roomType; } public double getRoomPrice() { return roomPrice; } public void setRoomPrice(double roomPrice) { this.roomPrice = roomPrice; } public int getHotelId() { return hotelId; } public void setHotelId(int hotelId) { this.hotelId = hotelId; } /* public int getBookingId() { return bookingId; } public void setBookingId(int bookingId) { this.bookingId = bookingId; } */ public boolean isBooked() { return isBooked; } public void setBooked(boolean isBooked) { this.isBooked = isBooked; } public void getId() { this.id = id; // TODO Auto-generated method stub } }
//database
package com.newton.holidaymaker.mockdata; import java.util.Arrays; import java.util.List; import com.newton.holidaymaker.models.Hotel; import com.newton.holidaymaker.models.Room; import com.newton.holidaymaker.models.User; import com.newton.holidaymaker.repositories.HotelRepository; import com.newton.holidaymaker.repositories.RoomRepository; import com.newton.holidaymaker.repositories.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.stereotype.Service; @Service public class DatabaseInit implements CommandLineRunner { @Autowired private UserRepository userRepository; @Autowired private HotelRepository hotelRepository; @Autowired private RoomRepository roomRepository; @Autowired private BCryptPasswordEncoder encoder; public DatabaseInit(UserRepository userRepository, BCryptPasswordEncoder encoder){ this.userRepository = userRepository; this.encoder = encoder; } @Override public void run(String... args) throws Exception { generateUsers(); generateHotels(); } /** * * Generates sample users * * */ public void generateUsers() { List<User> users = Arrays.asList( new User("Rafael", "Milanes", 1234, "ra@gmail.com", "Raf", encoder.encode("111"), "ADMIN","HOTEL_WRITE,HOTEL_READ,BOOKING_READ,BOOKING_WRITE,USER_READ,USER_WRITE,ROOM_READ,ROOM_WRITE"), new User("John", "Smith", 1234, "johnny@gmail.com", "John", encoder.encode("112"), "USER", "USER_READ"), new User("Ernest", "Hemingway", 3245, "ernest@gmail.com", "Ernest", encoder.encode("113"), "USER", "USER_READ"), new User("Donald", "Duck", 1111, "donald@duck.com", "donduck", encoder.encode("120"), "USER", "USER_READ"), new User("Bugs", "Bunny", 1112, "bugs@bunny.com", "bugsbunny", encoder.encode("121"), "USER", "USER_READ"), new User("Cookie", "Monster", 1113, "cookie@monster.com", "como1", encoder.encode("122"), "USER", "USER_READ"), new User("Pepe", "Greenfrog", 1113, "pepe@reee.com", "pepe", encoder.encode("123"), "USER", "USER_READ"), new User("Racecar", "McQueen", 1114, "idk@mail.com", "rcar", encoder.encode("124"), "USER", "USER_READ") ); if(userRepository.count() == 0) userRepository.saveAll(users); } /** * * Generates sample hotels & rooms * * */ public void generateHotels() { // ---------------- // Generate Hotels // ---------------- Hotel potato = new Hotel("Potato Hotel", "Sweden", "Mashedpotatoes 32"); Hotel tomato = new Hotel("Tomato Hotel", "Sweden", "Rottentomato 531"); Hotel cookie = new Hotel("Cookie Hotel", "Spain", "Cookiemonster 12"); Hotel pasta = new Hotel("Pasta Hotel", "Cuba", "Pastalavista 2"); List<Hotel> hotels = Arrays.asList(potato, tomato, cookie, pasta); if(hotelRepository.count() == 0) hotelRepository.saveAll(hotels); // --------------- // Generate Rooms // --------------- // POTATO HOTEL ROOMS List<Room> potatoRooms = Arrays.asList( new Room("Single", 100d, potato.getHotelID(), false), new Room("Double", 125d, potato.getHotelID(), true), new Room("Triple", 150d, potato.getHotelID(), true), new Room("Quad", 175d, potato.getHotelID(), false), new Room("King", 200d, potato.getHotelID(), false), new Room("Queen", 250d, potato.getHotelID(), true) ); // TOMATO HOTEL ROOMS List<Room> tomatoRooms = Arrays.asList( new Room("Single", 100d, tomato.getHotelID(), true), new Room("Double", 125d, tomato.getHotelID(), true), new Room("Triple", 150d, tomato.getHotelID(), false), new Room("Quad", 175d, tomato.getHotelID(), false), new Room("King", 200d, tomato.getHotelID(), false), new Room("Queen", 250d, tomato.getHotelID(), true) ); // COOKIE HOTEL ROOMS List<Room> cookieRooms = Arrays.asList( new Room("Single", 100d, cookie.getHotelID(), true), new Room("Double", 125d, cookie.getHotelID(), true), new Room("Triple", 150d, cookie.getHotelID(), true), new Room("Quad", 175d, cookie.getHotelID(), true), new Room("King", 200d, cookie.getHotelID(), true), new Room("Queen", 250d, cookie.getHotelID(), true) ); // PASTA HOTEL ROOMS List<Room> pastaRooms = Arrays.asList( new Room("Single", 100d, pasta.getHotelID(), true), new Room("Double", 125d, pasta.getHotelID(), false), new Room("Triple", 150d, pasta.getHotelID(), true), new Room("Quad", 175d, pasta.getHotelID(), false), new Room("King", 200d, pasta.getHotelID(), false), new Room("Queen", 250d, pasta.getHotelID(), false) ); if(roomRepository.count() == 0) { roomRepository.saveAll(potatoRooms); roomRepository.saveAll(tomatoRooms); roomRepository.saveAll(cookieRooms); roomRepository.saveAll(pastaRooms); } } }
Himai Minh
Bartender
Posts: 2266
13
posted 2 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi, Martin,
In your RoomController, you can add two methods for PUT and DELETE.
PUT is for updating an existing room or for adding a new room.
With a little knowledge, a
cast iron skillet
is non-stick and lasts a lifetime.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Is it possible to call a servlet function using JavaScript?
How to pass a parameter that is of type class into another class's constructor?
Cascading update over three entities with OpenJPA
@Autowired not working
Problem with SpringBoot in Eclipse
More...