Esempio n. 1
0
 /// <summary>
 /// Create a new Photo object.
 /// </summary>
 /// <param name="id">Initial value of the Id property.</param>
 /// <param name="userId">Initial value of the UserId property.</param>
 /// <param name="filePath">Initial value of the FilePath property.</param>
 /// <param name="createdAt">Initial value of the CreatedAt property.</param>
 public static Photo CreatePhoto(global::System.Int32 id, global::System.Guid userId, global::System.String filePath, global::System.DateTime createdAt)
 {
     Photo photo = new Photo();
     photo.Id = id;
     photo.UserId = userId;
     photo.FilePath = filePath;
     photo.CreatedAt = createdAt;
     return photo;
 }
Esempio n. 2
0
 /// <summary>
 /// Deprecated Method for adding a new object to the Photos EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToPhotos(Photo photo)
 {
     base.AddObject("Photos", photo);
 }
Esempio n. 3
0
        public ActionResult Upload(string var = "")
        {
            int viewPhotoId = 0;
            try {
                foreach (string photoFile in Request.Files)
                {
                    HttpPostedFileBase file = Request.Files[photoFile];

                    if (!(file.ContentLength > 0))
                    {
                        throw new Exception("File is not a valid image.");
                    }

                    string fileExtension = Path.GetExtension(Path.GetFileName(file.FileName));
                    string fileName = UserHelper.getLoggedInUserId() + "_" + DateTime.Now.Ticks.ToString() + fileExtension;
                    string filePath = Path.Combine(HttpContext.Server.MapPath("~/Uploads"), fileName);

                    file.SaveAs(filePath);

                    int albumId;

                    if(!int.TryParse(HttpContext.Request.Params["albumId"], out albumId))
                    {
                        throw new Exception("The requested album id is not a valid integer.");
                    }

                    Entities dbContext = new Entities();

                    var album = dbContext.Albums.SingleOrDefault(a => a.Id == albumId);

                    Guid userGuid = new Guid(UserHelper.getLoggedInUserId());

                    if(album == null || album.UserId != userGuid)
                    {
                        throw new Exception("The album you are trying to upload the photo to is not available.");
                    }

                    Models.Object obj = new Models.Object();
                    obj.EntityName = "Photo";
                    dbContext.Objects.AddObject(obj);
                    dbContext.SaveChanges();

                    Photo photo = new Photo();
                    photo.Id = obj.Id;
                    photo.AlbumId = album.Id;
                    photo.UserId = userGuid;
                    photo.FilePath = fileName;
                    photo.Caption = HttpContext.Request.Params["caption"];
                    photo.CreatedAt = DateTime.Now;
                    dbContext.Photos.AddObject(photo);
                    dbContext.SaveChanges();
                    viewPhotoId = photo.Id;
                }
                return RedirectToAction("ViewPhoto", "Photo", new { photoId = viewPhotoId });
            }catch(Exception ex)
            {
                return Content(ex.Message);
            }
        }