初始提交
This commit is contained in:
235
bin/controller/RoomController.dart
Normal file
235
bin/controller/RoomController.dart
Normal file
@@ -0,0 +1,235 @@
|
||||
import 'dart:convert';
|
||||
import 'package:EasyDartModule/EasyDartModule.dart';
|
||||
import 'package:EasyDartModule/base/webserver/WebServer.dart';
|
||||
import '../model/Room.dart';
|
||||
import '../service/RoomService.dart';
|
||||
import '../model/ApiResponse.dart';
|
||||
import '../const/MessageConstants.dart';
|
||||
import '../const/ResponseJsonCode.dart';
|
||||
import '../const/HttpStatusCode.dart';
|
||||
|
||||
part 'RoomController.route.dart';
|
||||
@RequestMapping(path: "/room")
|
||||
class RoomController {
|
||||
final RoomService roomService = RoomService();
|
||||
set callHandler(handler) => _callHandler = handler;
|
||||
Map<HttpMethod, List<List>> get routeMap => routes;
|
||||
|
||||
RoomController();
|
||||
|
||||
// 获取房间列表
|
||||
@RequestMapping(path: "/roomList", method: HttpMethod.GET)
|
||||
Future<Response> getRoomList(
|
||||
Request request, Map<String, dynamic> jwt) async {
|
||||
ApiResponse<List<Map<String, dynamic>>> apiResponse = ApiResponse();
|
||||
try {
|
||||
var queryParameters = request.requestedUri.queryParameters;
|
||||
Room query = Room.fromQueryParameters(queryParameters, jwt);
|
||||
List<dynamic> roomList = await roomService.getRoomList(query,
|
||||
page: queryParameters['page'] == null
|
||||
? 1
|
||||
: int.parse(queryParameters['page']!),
|
||||
pageSize: queryParameters['limit'] == null
|
||||
? 10
|
||||
: int.parse(queryParameters['limit']!));
|
||||
int roomTypeCount = await roomService.getRoomCount(query);
|
||||
List<Map<String, dynamic>> roomListMap =
|
||||
roomList.map((e) => e as Map<String, dynamic>).toList(); // 强制转换
|
||||
apiResponse.code = ResponseJsonCode.success;
|
||||
apiResponse.data = roomListMap;
|
||||
apiResponse.total = roomTypeCount;
|
||||
String serializedJson = apiResponse.serialize();
|
||||
return Response(HttpStatusCode.ok, body: serializedJson); // 发送响应
|
||||
} catch (e) {
|
||||
apiResponse.code = ResponseJsonCode.fail;
|
||||
apiResponse.message = "查询失败: $e"; // 包含异常信息
|
||||
String serializedJson = apiResponse.serialize();
|
||||
return Response(HttpStatusCode.internalServerError, body: serializedJson);
|
||||
}
|
||||
}
|
||||
|
||||
// 添加房间
|
||||
@RequestMapping(path: "/addRoom", method: HttpMethod.POST)
|
||||
Future<Response> addRoom(Request request) async {
|
||||
ApiResponse<String> apiResponse = ApiResponse();
|
||||
try {
|
||||
var data = jsonDecode(await request.readAsString());
|
||||
var room = Room.fromJson(data);
|
||||
var result = await roomService.addRoom(room);
|
||||
if (result == null || result.isEmpty) {
|
||||
apiResponse.code = ResponseJsonCode.success;
|
||||
apiResponse.message = MessageConstants.ADD_SUCCESS;
|
||||
String serializedJson = apiResponse.serialize();
|
||||
return Response(HttpStatusCode.ok, body: serializedJson);
|
||||
}
|
||||
apiResponse.code = ResponseJsonCode.fail;
|
||||
apiResponse.message = result;
|
||||
String serializedJson = apiResponse.serialize();
|
||||
return Response(HttpStatusCode.ok, body: serializedJson); // 发送成功响应
|
||||
} catch (e) {
|
||||
apiResponse.code = ResponseJsonCode.fail;
|
||||
apiResponse.message = "添加失败: $e"; // 包含异常信息
|
||||
String serializedJson = apiResponse.serialize();
|
||||
return Response(500, body: serializedJson); // 发送失败响应
|
||||
}
|
||||
}
|
||||
|
||||
// 更新房间
|
||||
@RequestMapping(path: "/updateRoom", method: HttpMethod.PUT)
|
||||
Future<Response> updateRoom(Request request) async {
|
||||
ApiResponse<String> apiResponse = ApiResponse();
|
||||
try {
|
||||
var data = jsonDecode(await request.readAsString());
|
||||
var roomId = data['_id'];
|
||||
var updatedRoom = Room.fromJson(data);
|
||||
if (updatedRoom.id == null || updatedRoom.id!.isEmpty) {
|
||||
apiResponse.code = ResponseJsonCode.fail;
|
||||
apiResponse.message = "房间类型ID不能为空";
|
||||
String serializedJson = apiResponse.serialize();
|
||||
return Response(HttpStatusCode.ok, body: serializedJson);
|
||||
}
|
||||
var result = await roomService.updateRoom(roomId, updatedRoom);
|
||||
apiResponse.code = 1;
|
||||
apiResponse.message = result;
|
||||
String serializedJson = apiResponse.serialize();
|
||||
return Response(200, body: serializedJson); // 发送成功响应
|
||||
} catch (e) {
|
||||
apiResponse.code = -1;
|
||||
apiResponse.message = "更新失败: $e"; // 包含异常信息
|
||||
String serializedJson = apiResponse.serialize();
|
||||
return Response(500, body: serializedJson); // 发送失败响应
|
||||
}
|
||||
}
|
||||
|
||||
// 删除房间
|
||||
@RequestMapping(path: "/deleteRoom", method: HttpMethod.DELETE)
|
||||
Future<Response> deleteRoom(Request request) async {
|
||||
ApiResponse<String> apiResponse = ApiResponse();
|
||||
try {
|
||||
var data = jsonDecode(await request.readAsString());
|
||||
var roomId = data['roomId'];
|
||||
var result = await roomService.deleteRoom(roomId);
|
||||
apiResponse.code = 1;
|
||||
if (result.isEmpty) {
|
||||
apiResponse.code = ResponseJsonCode.success;
|
||||
apiResponse.message = MessageConstants.DELETE_SUCCESS;
|
||||
String serializedJson = apiResponse.serialize();
|
||||
return Response(HttpStatusCode.ok, body: serializedJson); // 发送成功响应
|
||||
}
|
||||
apiResponse.code = ResponseJsonCode.fail;
|
||||
apiResponse.message = result;
|
||||
String serializedJson = apiResponse.serialize();
|
||||
return Response(HttpStatusCode.ok, body: serializedJson);
|
||||
} catch (e) {
|
||||
apiResponse.code = -1;
|
||||
apiResponse.message = "删除失败: $e"; // 包含异常信息
|
||||
String serializedJson = apiResponse.serialize();
|
||||
return Response(500, body: serializedJson); // 发送失败响应
|
||||
}
|
||||
}
|
||||
|
||||
//办理预约信息
|
||||
@RequestMapping(path: "/addOrderCheckIn", method: HttpMethod.POST)
|
||||
Future<Response> addOrderCheckIn(
|
||||
Request request, Map<String, dynamic> jwt) async {
|
||||
ApiResponse<String> apiResponse = ApiResponse();
|
||||
try {
|
||||
var data = jsonDecode(await request.readAsString());
|
||||
var result = await roomService.addOrderCheckIn(data,jwt);
|
||||
if (result == null || result.isEmpty) {
|
||||
apiResponse.code = ResponseJsonCode.success;
|
||||
apiResponse.message = MessageConstants.ADD_SUCCESS;
|
||||
String serializedJson = apiResponse.serialize();
|
||||
roomService.addReservationMessage(data, jwt);
|
||||
return Response(HttpStatusCode.ok, body: serializedJson);
|
||||
}
|
||||
apiResponse.code = ResponseJsonCode.fail;
|
||||
apiResponse.message = result;
|
||||
String serializedJson = apiResponse.serialize();
|
||||
return Response(HttpStatusCode.ok, body: serializedJson); // 发送成功响应
|
||||
} catch (e) {
|
||||
apiResponse.code = ResponseJsonCode.fail;
|
||||
apiResponse.message = "添加失败: $e"; // 包含异常信息
|
||||
String serializedJson = apiResponse.serialize();
|
||||
return Response(500, body: serializedJson); // 发送失败响应
|
||||
}
|
||||
}
|
||||
|
||||
//删除预约入住信息
|
||||
@RequestMapping(path: "/delOrderCheckIn", method: HttpMethod.POST)
|
||||
Future<Response> delOrderCheckIn(Request request) async {
|
||||
ApiResponse<String> apiResponse = ApiResponse();
|
||||
try {
|
||||
var data = jsonDecode(await request.readAsString());
|
||||
var result = await roomService.delOrderCheckIn(data);
|
||||
if (result == null || result.isEmpty) {
|
||||
apiResponse.code = ResponseJsonCode.success;
|
||||
apiResponse.message = MessageConstants.ADD_SUCCESS;
|
||||
String serializedJson = apiResponse.serialize();
|
||||
return Response(HttpStatusCode.ok, body: serializedJson);
|
||||
}
|
||||
apiResponse.code = ResponseJsonCode.fail;
|
||||
apiResponse.message = result;
|
||||
String serializedJson = apiResponse.serialize();
|
||||
return Response(HttpStatusCode.ok, body: serializedJson); // 发送成功响应
|
||||
} catch (e) {
|
||||
apiResponse.code = ResponseJsonCode.fail;
|
||||
apiResponse.message = "添加失败: $e"; // 包含异常信息
|
||||
String serializedJson = apiResponse.serialize();
|
||||
return Response(500, body: serializedJson); // 发送失败响应
|
||||
}
|
||||
}
|
||||
|
||||
//办理入住信息
|
||||
@RequestMapping(path: "/addRuzhuCheckIn", method: HttpMethod.POST)
|
||||
Future<Response> addRuzhuCheckIn(
|
||||
Request request, Map<String, dynamic> jwt) async {
|
||||
ApiResponse<String> apiResponse = ApiResponse();
|
||||
try {
|
||||
var data = jsonDecode(await request.readAsString());
|
||||
var result = await roomService.addRuzhuCheckIn(data, jwt);
|
||||
if (result == null || result.isEmpty) {
|
||||
apiResponse.code = ResponseJsonCode.success;
|
||||
apiResponse.message = MessageConstants.ADD_SUCCESS;
|
||||
String serializedJson = apiResponse.serialize();
|
||||
return Response(HttpStatusCode.ok, body: serializedJson);
|
||||
}
|
||||
apiResponse.code = ResponseJsonCode.fail;
|
||||
apiResponse.message = result;
|
||||
String serializedJson = apiResponse.serialize();
|
||||
return Response(HttpStatusCode.ok, body: serializedJson); // 发送成功响应
|
||||
} catch (e) {
|
||||
apiResponse.code = ResponseJsonCode.fail;
|
||||
apiResponse.message = "添加失败: $e"; // 包含异常信息
|
||||
String serializedJson = apiResponse.serialize();
|
||||
return Response(500, body: serializedJson); // 发送失败响应
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//办理退住信息
|
||||
@RequestMapping(path: "/checkout", method: HttpMethod.POST)
|
||||
Future<Response> checkout(
|
||||
Request request, Map<String, dynamic> jwt) async {
|
||||
ApiResponse<String> apiResponse = ApiResponse();
|
||||
try {
|
||||
var data = jsonDecode(await request.readAsString());
|
||||
var result = await roomService.checkout(data, jwt);
|
||||
if (result == null || result.isEmpty) {
|
||||
apiResponse.code = ResponseJsonCode.success;
|
||||
apiResponse.message = MessageConstants.ADD_SUCCESS;
|
||||
String serializedJson = apiResponse.serialize();
|
||||
return Response(HttpStatusCode.ok, body: serializedJson);
|
||||
}
|
||||
apiResponse.code = ResponseJsonCode.fail;
|
||||
apiResponse.message = result;
|
||||
String serializedJson = apiResponse.serialize();
|
||||
return Response(HttpStatusCode.ok, body: serializedJson); // 发送成功响应
|
||||
} catch (e) {
|
||||
apiResponse.code = ResponseJsonCode.fail;
|
||||
apiResponse.message = "添加失败: $e"; // 包含异常信息
|
||||
String serializedJson = apiResponse.serialize();
|
||||
return Response(500, body: serializedJson); // 发送失败响应
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user