Example #1
0
        // //POST api/Images?userId=1
        //public HttpResponseMessage PostImage(int userId, int albumId, Image image)
        //{
        //    Album album = this.albumRepository.Get(albumId);

        //    if (ModelState.IsValid && album.UserId == userId)
        //    {
        //        image.Album = album;
        //        image.AlbumId = album.AlbumId;
        //        this.imageRepository.Add(image);
        //        ImageModel imageModel = GetImage(userId, image.ImageId);

        //        HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, imageModel);
        //        response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = image.ImageId }));
        //        return response;
        //    }
        //    else
        //    {
        //        return Request.CreateResponse(HttpStatusCode.BadRequest);
        //    }
        //}

        //POST api/Images?userId=1
        public HttpResponseMessage PostImage(int userId, int albumId, Image image)
        {
            Album album = this.albumRepository.Get(albumId);

            string path     = HttpContext.Current.Server.MapPath("~/App_Data/");
            var    imageUrl = FileStorageAPI.UploadFile(image, path);

            if (ModelState.IsValid && album.UserId == userId)
            {
                image.Album   = album;
                image.AlbumId = album.AlbumId;
                image.URL     = imageUrl;
                this.imageRepository.Add(image);
                ImageModel imageModel = GetImage(userId, image.ImageId);

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, imageModel);
                response.Headers.Location = new Uri(imageUrl);
                //response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = image.ImageId }));
                return(response);
            }
            else
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }
        }
        //Post
        public HttpResponseMessage PostAlbum(int userId, Album album)
        {
            if (ModelState.IsValid && userId == album.UserId)
            {
                this.dbRepository.Add(album);
                FileStorageAPI.CreateFolder(album);
                string path = HttpContext.Current.Server.MapPath("~/App_Data/");

                foreach (var image in album.Images)
                {
                    FileStorageAPI.UploadFile(image, path);
                }

                var albumModel = new AlbumFullModel()
                {
                    AlbumID   = album.AlbumId,
                    AlbumName = album.AlbumName,
                    UserId    = album.UserId,
                    Images    =
                        from image in album.Images
                        select new ImageModel()
                    {
                        ImageId   = image.ImageId,
                        ImageName = image.ImageName,
                        URL       = image.URL
                    },
                    Albums =
                        from subAlbum in album.Albums
                        select new AlbumModel()
                    {
                        AlbumId     = subAlbum.AlbumId,
                        AlbumName   = subAlbum.AlbumName,
                        AlbumCount  = subAlbum.Albums.Count,
                        ImagesCount = subAlbum.Images.Count
                    }
                };
                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, albumModel);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = album.AlbumId }));
                return(response);
            }
            else
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }
        }