示例#1
0
        public async Task <IHttpActionResult> Put(string id, LoaiPhongViewModel.Dto instance)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != instance.ID)
            {
                return(BadRequest());
            }
            var result = new TransferObj <LOAIPHONG>();

            if (id != instance.ID)
            {
                result.Status  = false;
                result.Message = "Mã ID không hợp lệ";
                return(Ok(result));
            }
            try
            {
                var    data = Mapper.Map <LoaiPhongViewModel.Dto, LOAIPHONG>(instance);
                string path = _service.PhysicalPathUploadLoaiPhong() + data.MALOAIPHONG + "\\";
                if (!string.IsNullOrEmpty(instance.BACKGROUND_NAME))
                {
                    FileStream fs             = new FileStream(path + instance.BACKGROUND_NAME, FileMode.Open, FileAccess.Read);
                    byte[]     BackgroundData = new byte[fs.Length];
                    fs.Read(BackgroundData, 0, System.Convert.ToInt32(fs.Length));
                    fs.Close();
                    data.BACKGROUND = BackgroundData;
                }

                if (!string.IsNullOrEmpty(instance.ICON_NAME))
                {
                    FileStream fs       = new FileStream(path + instance.ICON_NAME, FileMode.Open, FileAccess.Read);
                    byte[]     IconData = new byte[fs.Length];
                    fs.Read(IconData, 0, System.Convert.ToInt32(fs.Length));
                    fs.Close();
                    data.ICON = IconData;
                }
                var item = _service.Update(data);
                int upd  = await _service.UnitOfWork.SaveAsync();

                if (upd > 0)
                {
                    result.Status  = true;
                    result.Data    = item;
                    result.Message = "Cập nhật thành công";
                }
                else
                {
                    result.Status  = false;
                    result.Data    = null;
                    result.Message = "Thao tác không thành công";
                }
            }
            catch (Exception e)
            {
                result.Data    = null;
                result.Status  = false;
                result.Message = e.Message;
            }
            return(Ok(result));
        }
示例#2
0
        public async Task <IHttpActionResult> Post(LoaiPhongViewModel.Dto instance)
        {
            var result         = new TransferObj <LOAIPHONG>();
            var curentUnitCode = _service.GetCurrentUnitCode();

            if (instance.MALOAIPHONG == "")
            {
                result.Status  = false;
                result.Message = "Mã không hợp lệ";
                return(Ok(result));
            }
            else
            {
                var exist = _service.Repository.DbSet.FirstOrDefault(x => x.MALOAIPHONG == instance.MALOAIPHONG && x.UNITCODE.Equals(curentUnitCode));
                if (exist != null)
                {
                    result.Status  = false;
                    result.Message = "Đã tồn tại loại phòng này";
                    return(Ok(result));
                }
            }
            try
            {
                instance.MALOAIPHONG = _service.SaveCode();
                var    data = Mapper.Map <LoaiPhongViewModel.Dto, LOAIPHONG>(instance);
                string path = _service.PhysicalPathUploadLoaiPhong() + data.MALOAIPHONG + "\\";
                if (!string.IsNullOrEmpty(instance.BACKGROUND_NAME))
                {
                    FileStream fs             = new FileStream(path + instance.BACKGROUND_NAME, FileMode.Open, FileAccess.Read);
                    byte[]     BackgroundData = new byte[fs.Length];
                    fs.Read(BackgroundData, 0, System.Convert.ToInt32(fs.Length));
                    fs.Close();
                    data.BACKGROUND = BackgroundData;
                }

                if (!string.IsNullOrEmpty(instance.ICON_NAME))
                {
                    FileStream fs       = new FileStream(path + instance.ICON_NAME, FileMode.Open, FileAccess.Read);
                    byte[]     IconData = new byte[fs.Length];
                    fs.Read(IconData, 0, System.Convert.ToInt32(fs.Length));
                    fs.Close();
                    data.ICON = IconData;
                }
                var item = _service.Insert(data);
                int inst = await _service.UnitOfWork.SaveAsync();

                if (inst > 0)
                {
                    result.Status  = true;
                    result.Data    = item;
                    result.Message = "Thêm mới thành công";
                }
                else
                {
                    result.Status  = false;
                    result.Data    = null;
                    result.Message = "Thao tác không thành công";
                }
            }
            catch (Exception e)
            {
                result.Status  = false;
                result.Message = e.Message;
            }
            return(Ok(result));
        }