117 lines
3.6 KiB
C#
117 lines
3.6 KiB
C#
using Model;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data.Entity;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.ServiceModel.Channels;
|
|
using System.Threading.Tasks;
|
|
using System.Web;
|
|
using System.Web.Http;
|
|
using static regionController;
|
|
|
|
/// <summary>
|
|
/// regionRoomController 的摘要描述
|
|
/// </summary>
|
|
[ezAuthorize]
|
|
public class regionRoomController : ApiController
|
|
{
|
|
private Model.ezEntities _db = new Model.ezEntities();
|
|
public regionRoomController()
|
|
{
|
|
//
|
|
// TODO: 在這裡新增建構函式邏輯
|
|
//
|
|
}
|
|
[HttpGet]
|
|
[Route("api/region/room/get")]
|
|
public async Task<IHttpActionResult> getRoomList()
|
|
{
|
|
var rooms = await _db.Room.ToListAsync();
|
|
return Ok(rooms);
|
|
}
|
|
[HttpPost]
|
|
[Route("api/region/room/create")]
|
|
public async Task<IHttpActionResult> createRoom([FromBody] Room room)
|
|
{
|
|
if (!room.BedCount.HasValue)
|
|
{
|
|
return BadRequest("請輸入床位數量");
|
|
}
|
|
var newRoom = new Room();
|
|
newRoom.Name = room.Name;
|
|
newRoom.RegionUuid = room.RegionUuid;
|
|
newRoom.BedCount = room.BedCount;
|
|
newRoom.CreatedAt = room.CreatedAt;
|
|
newRoom.Gender = room.Gender;
|
|
newRoom.IsActive = room.IsActive;
|
|
newRoom.Uuid = Guid.NewGuid();
|
|
_db.Room.Add(newRoom);
|
|
await _db.SaveChangesAsync();
|
|
var Rooms = new
|
|
{
|
|
uuid = newRoom.Uuid,
|
|
Name = newRoom.Name,
|
|
regionUuid = newRoom.RegionUuid,
|
|
Gender = newRoom.Gender,
|
|
BedCount = newRoom.BedCount,
|
|
IsActive = newRoom.IsActive,
|
|
beds = newRoom.RegionRoomBed.Select(c => new BedDto
|
|
{
|
|
Uuid = c.Uuid,
|
|
name = c.Name,
|
|
roomUuid = c.RoomUuid,
|
|
isactive = c.IsActive,
|
|
statuscode = c.StatusCode,
|
|
}).ToList(),
|
|
|
|
};
|
|
return Ok(Rooms);
|
|
}
|
|
[HttpPost]
|
|
[Route("api/region/room/update")]
|
|
public async Task<IHttpActionResult> updateRoom([FromBody] Room room)
|
|
{
|
|
var oldRoom = await _db.Room.FindAsync(room.Uuid);
|
|
if (oldRoom == null)
|
|
{
|
|
return BadRequest("未找到该房间信息,更新失败");
|
|
}
|
|
// 判斷房間中是否存在與房間性別不符的床位
|
|
bool canProceed = !_db.RegionRoomBed
|
|
.Any(a => a.RoomUuid == room.Uuid && a.Gender != room.Gender);
|
|
|
|
if (!canProceed)
|
|
{
|
|
// 如果有不符合性別的床位,不能繼續操作
|
|
return BadRequest("房間中已有與房間性別不符的床位,無法操作");
|
|
}
|
|
if (!room.BedCount.HasValue)
|
|
{
|
|
return BadRequest("請輸入床位數量");
|
|
}
|
|
oldRoom.Name = room.Name;
|
|
oldRoom.BedCount = room.BedCount;
|
|
oldRoom.Gender = room.Gender;
|
|
oldRoom.UpdatedAt = DateTime.Now;
|
|
oldRoom.IsActive = room.IsActive;
|
|
oldRoom.RegionUuid = room.RegionUuid;
|
|
await _db.SaveChangesAsync();
|
|
return Ok(new { message = "更新成功" });
|
|
}
|
|
[HttpPost]
|
|
[Route("api/region/room/delete")]
|
|
public async Task<IHttpActionResult> deleteRoom([FromBody] Room rm)
|
|
{
|
|
var room = await _db.Room.FindAsync(rm.Uuid);
|
|
if (room == null) return BadRequest("房間不存在");
|
|
|
|
var beds = _db.RegionRoomBed.Where(b => b.RoomUuid == room.Uuid);
|
|
_db.RegionRoomBed.RemoveRange(beds);
|
|
|
|
_db.Room.Remove(room);
|
|
await _db.SaveChangesAsync();
|
|
return Ok(new { message = "刪除成功" });
|
|
}
|
|
|
|
} |