mirror of
				https://github.com/FreeeBird/hotel.git
				synced 2025-10-31 12:34:54 +08:00 
			
		
		
		
	完成房间类型接口编写,通过单元测试
This commit is contained in:
		| @@ -1,5 +1,6 @@ | ||||
| package cn.mafangui.hotel; | ||||
|  | ||||
| import cn.mafangui.hotel.utils.StaticString; | ||||
| import org.mybatis.spring.annotation.MapperScan; | ||||
| import org.springframework.boot.SpringApplication; | ||||
| import org.springframework.boot.autoconfigure.SpringBootApplication; | ||||
|   | ||||
| @@ -0,0 +1,43 @@ | ||||
| package cn.mafangui.hotel.controller; | ||||
|  | ||||
| import cn.mafangui.hotel.entity.Room; | ||||
| import cn.mafangui.hotel.entity.RoomType; | ||||
| import cn.mafangui.hotel.service.RoomService; | ||||
| import cn.mafangui.hotel.service.RoomTypeService; | ||||
| import cn.mafangui.hotel.service.WorkerService; | ||||
| import cn.mafangui.hotel.utils.StaticString; | ||||
| import org.springframework.beans.factory.annotation.Autowired; | ||||
| import org.springframework.web.bind.annotation.RequestMapping; | ||||
| import org.springframework.web.bind.annotation.RequestMethod; | ||||
| import org.springframework.web.bind.annotation.RestController; | ||||
|  | ||||
| import java.util.List; | ||||
|  | ||||
| @RestController | ||||
| @RequestMapping(value = "/admin") | ||||
| public class AdminController { | ||||
|  | ||||
|     @Autowired | ||||
|     private WorkerService workerService; | ||||
|     @Autowired | ||||
|     private RoomService roomService; | ||||
|  | ||||
|  | ||||
|     /** | ||||
|      * 管理员登录 | ||||
|      * @param username | ||||
|      * @param password | ||||
|      * @return | ||||
|      */ | ||||
|     @RequestMapping(method = RequestMethod.POST,value = "/login") | ||||
|     public int login(String username,String password){ | ||||
|         if(workerService.login(username,password, StaticString.ADMIN) != null) | ||||
|             return 1; | ||||
|         else return 0; | ||||
|     } | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
| } | ||||
| @@ -0,0 +1,30 @@ | ||||
| package cn.mafangui.hotel.controller; | ||||
|  | ||||
| import cn.mafangui.hotel.service.WorkerService; | ||||
| import cn.mafangui.hotel.utils.StaticString; | ||||
| import org.springframework.beans.factory.annotation.Autowired; | ||||
| import org.springframework.web.bind.annotation.RequestMapping; | ||||
| import org.springframework.web.bind.annotation.RequestMethod; | ||||
| import org.springframework.web.bind.annotation.RestController; | ||||
|  | ||||
| @RestController | ||||
| @RequestMapping(value = "/operator") | ||||
| public class OperatorController { | ||||
|     @Autowired | ||||
|     private WorkerService workerService; | ||||
|  | ||||
|     /** | ||||
|      * 管理员登录 | ||||
|      * @param username | ||||
|      * @param password | ||||
|      * @return | ||||
|      */ | ||||
|     @RequestMapping(method = RequestMethod.POST,value = "/login") | ||||
|     public int login(String username,String password){ | ||||
|         if(workerService.login(username,password, StaticString.OPERATOR) != null) | ||||
|             return 1; | ||||
|         else return 0; | ||||
|     } | ||||
|  | ||||
|  | ||||
| } | ||||
| @@ -0,0 +1,25 @@ | ||||
| package cn.mafangui.hotel.controller; | ||||
|  | ||||
| import cn.mafangui.hotel.entity.Room; | ||||
| import cn.mafangui.hotel.service.RoomService; | ||||
| import org.springframework.beans.factory.annotation.Autowired; | ||||
| import org.springframework.web.bind.annotation.RequestMapping; | ||||
| import org.springframework.web.bind.annotation.RestController; | ||||
|  | ||||
| import java.util.List; | ||||
|  | ||||
| @RestController | ||||
| @RequestMapping(value = "/room") | ||||
| public class RoomController { | ||||
|  | ||||
|     @Autowired | ||||
|     private RoomService roomService; | ||||
|  | ||||
|     @RequestMapping(value = "/all") | ||||
|     public List<Room> getAllRoom(){ | ||||
|         List<Room> result = roomService.selectAll(); | ||||
|         for (Room r:result) { | ||||
|         } | ||||
|         return result; | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,51 @@ | ||||
| package cn.mafangui.hotel.controller; | ||||
|  | ||||
| import cn.mafangui.hotel.entity.RoomType; | ||||
| import cn.mafangui.hotel.service.RoomTypeService; | ||||
| import org.springframework.beans.factory.annotation.Autowired; | ||||
| import org.springframework.web.bind.annotation.RequestMapping; | ||||
| import org.springframework.web.bind.annotation.RequestMethod; | ||||
| import org.springframework.web.bind.annotation.RestController; | ||||
|  | ||||
| import java.util.List; | ||||
|  | ||||
| @RestController | ||||
| @RequestMapping(value = "/roomType") | ||||
| public class RoomTypeController { | ||||
|  | ||||
|     @Autowired | ||||
|     private RoomTypeService roomTypeService; | ||||
|  | ||||
|  | ||||
|     @RequestMapping(value = "/all") | ||||
|     public List<RoomType> getAllRoomType(){ | ||||
|         return roomTypeService.findAllType(); | ||||
|     } | ||||
|  | ||||
|     @RequestMapping(method = RequestMethod.POST,value = "/add") | ||||
|     public int addRoomType(String roomType,Double price,Double discount,int area, | ||||
|                            int bedNum,String bedSize,int window,String remark){ | ||||
|         RoomType rt = new RoomType(roomType,remark,price,discount,area,bedNum,bedSize,window); | ||||
|         int result = 0; | ||||
|         result = roomTypeService.insert(rt); | ||||
|         return result; | ||||
|     } | ||||
|  | ||||
|     @RequestMapping(method = RequestMethod.POST,value = "/update") | ||||
|     public int updateRoomType(int typeId,String roomType,Double price,Double discount,int area, | ||||
|                            int bedNum,String bedSize,int window,String remark){ | ||||
|         RoomType rt = new RoomType(roomType,remark,price,discount,area,bedNum,bedSize,window); | ||||
|         rt.setTypeId(typeId); | ||||
|         int result = 0; | ||||
|         result = roomTypeService.update(rt); | ||||
|         return result; | ||||
|     } | ||||
|  | ||||
|     @RequestMapping(method = RequestMethod.POST,value = "/delete") | ||||
|     public int deleteRoomType(int typeId){ | ||||
|         int result = 0; | ||||
|         result = roomTypeService.delete(typeId); | ||||
|         return result; | ||||
|     } | ||||
|  | ||||
| } | ||||
| @@ -113,6 +113,20 @@ public class RoomType { | ||||
|         this.updateTime = updateTime; | ||||
|     } | ||||
|  | ||||
|     public RoomType() { | ||||
|     } | ||||
|  | ||||
|     public RoomType(String roomType, String remark, Double price, Double discount, Integer area, Integer bedNum, String bedSize, Integer window) { | ||||
|         this.roomType = roomType; | ||||
|         this.remark = remark; | ||||
|         this.price = price; | ||||
|         this.discount = discount; | ||||
|         this.area = area; | ||||
|         this.bedNum = bedNum; | ||||
|         this.bedSize = bedSize; | ||||
|         this.window = window; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public String toString() { | ||||
|         return "RoomType{" + | ||||
|   | ||||
| @@ -20,7 +20,7 @@ public interface WorkerMapper { | ||||
|  | ||||
|     int updateByPrimaryKey(Worker record); | ||||
|  | ||||
|     Worker selectByUsernameAndPassword(@Param("username") String username, @Param("password") String password); | ||||
|     Worker selectByUsernameAndPassword(@Param("username") String username, @Param("password") String password, @Param("role") String role); | ||||
|  | ||||
|     List<Worker> selectByRole(String role); | ||||
|  | ||||
|   | ||||
| @@ -11,5 +11,5 @@ public interface WorkerService { | ||||
|     Worker selectById(int workerId); | ||||
|     List<Worker> findAll(); | ||||
|     List<Worker> selectByRole(String role); | ||||
|     Worker login(String username,String password); | ||||
|     Worker login(String username,String password,String role); | ||||
| } | ||||
|   | ||||
| @@ -45,7 +45,7 @@ public class WorkerServiceImpl implements WorkerService { | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public Worker login(String username, String password) { | ||||
|         return workerMapper.selectByUsernameAndPassword(username,password); | ||||
|     public Worker login(String username, String password,String role) { | ||||
|         return workerMapper.selectByUsernameAndPassword(username,password,role); | ||||
|     } | ||||
| } | ||||
|   | ||||
							
								
								
									
										17
									
								
								src/main/java/cn/mafangui/hotel/utils/RoomStaticUtil.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								src/main/java/cn/mafangui/hotel/utils/RoomStaticUtil.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,17 @@ | ||||
| package cn.mafangui.hotel.utils; | ||||
|  | ||||
| public class RoomStaticUtil { | ||||
|     public static final int UNAVAILABLE = 0; | ||||
|     public static final int AVAILABLE = 1; | ||||
|     public static final int OCCUPIED = 2; | ||||
|     public static final int IN_USE = 3; | ||||
|     public static final String[] STATUS = {"UNAVAILABLE","AVAILABLE","OCCUPIED","IN_USE",}; | ||||
|  | ||||
|     public RoomStaticUtil(){ | ||||
|  | ||||
|     } | ||||
|     public static String getRoomStatic(){ | ||||
|  | ||||
|         return null; | ||||
|     } | ||||
| } | ||||
| @@ -1,5 +1,7 @@ | ||||
| package cn.mafangui.hotel.utils; | ||||
|  | ||||
| import java.util.Map; | ||||
|  | ||||
| public class StaticString { | ||||
|     public static final String CODE = "code"; | ||||
|     public static final String STATUS = "status"; | ||||
| @@ -10,4 +12,16 @@ public class StaticString { | ||||
|      */ | ||||
|     public static final String ADMIN = "admin"; | ||||
|     public static final String OPERATOR = "operator"; | ||||
|  | ||||
|     /** | ||||
|      * 房间状态 | ||||
|      */ | ||||
|     public static final int AVAILABLE = 1; | ||||
|     public static final int OCCUPIED = 0; | ||||
|     public static final int IN_USE = -1; | ||||
|     public static final int UNAVAILABLE = -2; | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -27,7 +27,7 @@ | ||||
|   </select> | ||||
|   <select id="selectByUsernameAndPassword" parameterType="String" resultMap="BaseResultMap"> | ||||
|     select * from worker_info | ||||
|     where username = #{username,jdbcType=VARCHAR} and password = #{password,jdbcType=VARCHAR} | ||||
|     where username = #{username,jdbcType=VARCHAR} and password = #{password,jdbcType=VARCHAR} and role = #{role,jdbcType=VARCHAR} | ||||
|   </select> | ||||
|   <select id="selectByRole" parameterType="String" resultMap="BaseResultMap"> | ||||
|     select * from worker_info | ||||
|   | ||||
| @@ -0,0 +1,49 @@ | ||||
| package cn.mafangui.hotel.controller; | ||||
|  | ||||
| import cn.mafangui.hotel.entity.RoomType; | ||||
| import org.junit.Assert; | ||||
| import org.junit.Test; | ||||
| import org.junit.runner.RunWith; | ||||
| import org.springframework.beans.factory.annotation.Autowired; | ||||
| import org.springframework.boot.test.context.SpringBootTest; | ||||
| import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||||
|  | ||||
| import java.util.List; | ||||
|  | ||||
| import static org.junit.Assert.*; | ||||
|  | ||||
| @SpringBootTest | ||||
| @RunWith(SpringJUnit4ClassRunner.class) | ||||
| public class RoomTypeControllerTest { | ||||
|  | ||||
|     @Autowired | ||||
|     RoomTypeController rtc; | ||||
|  | ||||
|     @Test | ||||
|     public void getAllRoomType() { | ||||
|         List<RoomType> result = rtc.getAllRoomType(); | ||||
|         System.out.println(result); | ||||
|         Assert.assertEquals(2,result.size()); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void addRoomType() { | ||||
|         int res = 0; | ||||
|         res = rtc.addRoomType("单人房",129.0,12.0,10,1,"1.2",0,"1 Person"); | ||||
|         Assert.assertEquals(1,res); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void updateRoomType() { | ||||
|         int res = 0; | ||||
|         res = rtc.updateRoomType(2,"单人房",109.0,22.0,10,1,"1.2",0,"1 Person"); | ||||
|         Assert.assertEquals(1,res); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void deleteRoomType() { | ||||
|         int res = 0; | ||||
|         res = rtc.deleteRoomType(1); | ||||
|         Assert.assertEquals(1,res); | ||||
|     } | ||||
| } | ||||
| @@ -94,7 +94,7 @@ public class WorkerServiceImplTest { | ||||
|     public void login() { | ||||
|         String username = "admin"; | ||||
|         String password = "admin"; | ||||
|         Worker worker = workerService.login(username,password); | ||||
|         Worker worker = workerService.login(username,password,"admin"); | ||||
|         Assert.assertNotNull(worker); | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user