Example #1
0
        public ApiResult <HotelModels> OwnerUpdate(PutHotel Item, int UserId)
        {
            ApiResult <HotelModels> Rs = new ApiResult <HotelModels>();

            try
            {
                DbProvider.SetCommandText("sp_Hotel_Owner_Update", CommandType.StoredProcedure);
                DbProvider.AddParameter("InJson", Libs.SerializeObject(Item), SqlDbType.NVarChar);
                DbProvider.AddParameter("UserId", UserId, SqlDbType.Int);

                // Output params
                DbProvider.AddParameter("ErrorCode", DBNull.Value, SqlDbType.NVarChar, 100, ParameterDirection.Output);
                DbProvider.AddParameter("ReturnMsg", DBNull.Value, SqlDbType.NVarChar, 4000, ParameterDirection.Output);

                DbProvider.ExecuteNonQuery();

                // Kiểm tra kết quả trả về
                string errorCode = DbProvider.Command.Parameters["ErrorCode"].Value.ToString();
                if (!errorCode.Equals(Constants.SUCCESS))
                {
                    Rs.Failed(new ErrorObject()
                    {
                        Code        = DbProvider.Command.Parameters["ErrorCode"].Value.ToString(),
                        Description = DbProvider.Command.Parameters["ReturnMsg"].Value.ToString()
                    });
                }
            }
            catch (Exception ex)
            {
                Rs.Failed(new ErrorObject()
                {
                    Code        = Constants.ERR_EXCEPTION,
                    Description = ex.Message
                });
            }

            return(Rs);
        }
Example #2
0
        public IHttpActionResult OwnerUpdate([FromBody] PutHotel Item)
        {
            ApiResult <HotelModels> Rs = new ApiResult <HotelModels>();

            if (Item == null)
            {
                Rs.Failed(new ErrorObject
                {
                    Code        = Constants.ERR_EXCEPTION,
                    Description = "Đéo nhận Data truyền vào"
                });
                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));
            }
            int UserId = UserInfo.Id;

            UploadFileResult UploadAvatar     = null;
            UploadFileResult UploadCoverImage = null;

            if (Item.Avatar != null && Item.Avatar.StartsWith("data:image") && Item.Avatar.IndexOf(";base64,") > -1)
            {
                UploadAvatar = CommonUtil.UploadBase64File(
                    Item.Avatar,
                    string.Format("{0}_{1}.png", Item.Name.Replace(" ", "_"), "Avatar"),
                    string.Format(ConfigUtil.GetConfigurationValueFromKey("HotelAvatarDerectory", false), Item.Id),
                    ImageFormat.Png,
                    20
                    );

                if (UploadAvatar != null && !UploadAvatar.HasError)
                {
                    Item.Avatar = UploadAvatar.FilePath;
                }
                else
                {
                    Rs.Failed(new ErrorObject
                    {
                        Code        = Constants.ERR_EXCEPTION,
                        Description = "Cannot Upload Avatar"
                    });

                    return(Content(HttpStatusCode.BadRequest, Rs));
                }
            }

            if (Item.CoverImage != null && Item.CoverImage.StartsWith("data:image") && Item.CoverImage.IndexOf(";base64,") > -1)
            {
                UploadCoverImage = CommonUtil.UploadBase64File(
                    Item.CoverImage,
                    string.Format("{0}_{1}.png", Item.Name.Replace(" ", "_"), "CoverImage"),
                    string.Format(ConfigUtil.GetConfigurationValueFromKey("HotelCoverDerectory", false), Item.Id),
                    ImageFormat.Png,
                    20
                    );

                if (UploadCoverImage != null && !UploadCoverImage.HasError)
                {
                    Item.CoverImage = UploadCoverImage.FilePath;
                }
                else
                {
                    Rs.Failed(new ErrorObject
                    {
                        Code        = Constants.ERR_EXCEPTION,
                        Description = "Cannot Upload Cover Image"
                    });

                    return(Content(HttpStatusCode.BadRequest, Rs));
                }
            }

            if (Item.Images != null && Item.Images.Count > 0)
            {
                foreach (var item in Item.Images.Select((value, i) => new { i, value }))
                {
                    int Index                 = item.i;
                    var E                     = item.value;
                    UploadFileResult rt       = null;
                    string           FileName = RandomString(16) + ".png";

                    try
                    {
                        rt = CommonUtil.UploadBase64File(
                            E,
                            FileName,
                            string.Format(ConfigUtil.GetConfigurationValueFromKey("HotelAlbumDirectory", false), Item.Id),
                            ImageFormat.Png,
                            20
                            );
                    }
                    catch (Exception)
                    {
                        continue;
                    }

                    if (rt != null && !rt.HasError)
                    {
                        //Item.Images[Index] = "";
                        FileUploadCompact fi = new FileUploadCompact
                        {
                            FIleName = FileName,
                            Url      = rt.FilePath
                        };
                        Item.Album.Add(fi);
                    }
                    else
                    {
                        continue;
                    }
                }
                Item.Images = null;
            }

            Rs = hotelDAL.OwnerUpdate(Item, UserId);

            if (!Rs.Succeeded)
            {
                return(Content(HttpStatusCode.BadRequest, Rs));
            }

            return(Ok(Rs));
        }