示例#1
0
        public ActionResult Create(PropertyViewModel p)
        {
            PropertyRepository cc = new PropertyRepository();

            try
            {
                if (cc.MyConnection.State == System.Data.ConnectionState.Closed)
                {
                    cc.MyConnection.Open();
                }

                //save in bucket
                var myStorageManager = StorageClient.Create();
                var guid             = Guid.NewGuid();
                var mynewobj         = myStorageManager.UploadObject("programmingcloud", guid.ToString() + Path.GetExtension(p.File.FileName), p.File.ContentType, p.File.InputStream,
                                                                     options: new Google.Cloud.Storage.V1.UploadObjectOptions()
                {
                    PredefinedAcl = Google.Cloud.Storage.V1.PredefinedObjectAcl.PublicRead
                });

                string imgName = guid.ToString() + Path.GetExtension(p.File.FileName);
                string us      = User.Identity.Name;
                string u       = us.Substring(0, us.IndexOf("@"));
                p.Username = u;
                var prop = new Property()
                {
                    Username        = p.Username,
                    Location        = p.Location,
                    Name            = p.Name,
                    Description     = p.Description,
                    Price           = p.Price,
                    PropertyPicture = imgName
                };
                cc.AddProperty(prop);


                ViewBag.Success = "Property was created successfully";
                return(View());
            }
            catch (Exception ex)
            {
                LoggingRepository.ReportError(ex);
                ViewBag.Error = ex + " - (Error occurred. Property was not added)";
                return(View());
            }
            finally
            {
                if (cc.MyConnection.State == System.Data.ConnectionState.Open)
                {
                    cc.MyConnection.Close();
                }
            }
        }