コード例 #1
0
        //valida e insere na tabela
        public Response Register(Room room)
        {
            Response result = Validate(room);

            if (!result.Success)
            {
                return(result);
            }


            Response resultInsert = _roomDAL.Insert(room);

            if (!resultInsert.Success)
            {
                return(resultInsert);
            }

            return(Response.CreateSuccess("Quarto cadastrado com sucesso"));
        }
コード例 #2
0
        public IHttpActionResult Insert(RoomDetail Item)
        {
            ApiResult <RoomResult> rs = new ApiResult <RoomResult>();

            if (Item == null)
            {
                rs.Failed(new ErrorObject
                {
                    Code        = "EXCEPTION",
                    Description = "Data not Found"
                });
                return(Content(HttpStatusCode.BadRequest, rs));
            }
            if (!ModelState.IsValid)
            {
                // Lỗi validate dữ liệu trả ra từ model
                foreach (string key in ModelState.Keys)
                {
                    ModelState current = ModelState[key];
                    foreach (ModelError error in current.Errors)
                    {
                        rs.Failed(new ErrorObject()
                        {
                            Code        = key,
                            Description = error.ErrorMessage
                        });
                    }
                }

                return(Content(HttpStatusCode.BadRequest, rs));
                //IEnumerable<ModelError> a = ModelState.Values.SelectMany(x => x.Errors);

                //return Content(HttpStatusCode.BadRequest, a);
            }


            UploadFileResult uploadLogo = null;

            if (Item.Avatar.StartsWith("data:image"))
            {
                uploadLogo = CommonUtil.UploadBase64File(
                    Item.Avatar,
                    string.Format("{0}_{1}.png", Item.Name.Replace(" ", "_"), "Avatar"),
                    ConfigUtil.GetConfigurationValueFromKey("RoomAvatarDerectory", false),
                    ImageFormat.Png,
                    20
                    );

                if (uploadLogo != null && !uploadLogo.HasError)
                {
                    Item.Avatar = uploadLogo.FilePath;
                }
            }


            rs = roomDAL.Insert(Item, UserInfo.Id);
            if (!rs.Succeeded)
            {
                return(Content(HttpStatusCode.BadRequest, rs));
            }
            return(Ok(rs));
        }