예제 #1
0
        public IHttpActionResult Delete(Guid id)
        {
            var file = UploadService.GetFile(id);

            if (file == null)
            {
                return(BadRequest("File not found"));
            }
            var path = Path.Combine(BaseFilePath, file.Template ? "000" : CompanyId.ToString(), file.SoaChapterId.ToString(), file.FileName);

            if (File.Exists(path))
            {
                File.Delete(path);
            }

            UploadService.DeleteFile(file);
            return(Ok());
        }
        public ChromelyResponse UpdateMovie(ChromelyRequest req)
        {
            JObject data = (JObject)JsonConvert.DeserializeObject(req.PostData.ToJson());

            //convert kijkwijzer collection to int array
            int[] kijkWijzers;
            //get base64 string of thumbnail image
            string thumbnail;
            //get base64 string of cover image
            string filestring;
            int    id;

            try
            {
                try
                {
                    kijkWijzers = data["kijkwijzers"].Select(x => (int)x).ToArray();
                } catch (ArgumentNullException)
                {
                    return(Response.ParseError(req.Id));
                }
                thumbnail  = data["thumbnail_image"].Value <string>();
                filestring = data["cover_image"].Value <string>();
                id         = data.Value <int>("id");
            } catch (FormatException)
            {
                return(Response.ParseError(req.Id));
            }

            Repository <Movie> repository = new Repository <Movie>();
            string             filename;
            string             thumbnailName;

            try
            {
                filename      = repository.Data[id].coverImage;
                thumbnailName = repository.Data[id].thumbnailImage;
            } catch (KeyNotFoundException)
            {
                return(Response.IllegalUpdate(req.Id, "No movie with specified Id found."));
            } catch (InvalidOperationException)
            {
                return(Response.TransactionProtocolViolation(req.Id));
            }

            if (filestring.Length > 0)
            {
                var uploadService = new UploadService(filestring);
                if (uploadService.CheckIsImage())
                {
                    uploadService.DeleteFile(filename);
                    uploadService.CreateFileInUploadFolder();
                    filename = uploadService.GetFileName();
                }
            }

            if (thumbnail.Length != 0)
            {
                var uploadService = new UploadService(thumbnail);
                if (uploadService.CheckIsImage())
                {
                    uploadService.DeleteFile(thumbnailName);
                    uploadService.CreateFileInUploadFolder();
                    thumbnailName = uploadService.GetFileName();
                }
            }

            try
            {
                repository.Update(data.Value <int>("id"), new Movie(
                                      data["title"].Value <string>(),
                                      data["genre"].Value <string>(),
                                      data["rating"].Value <double>(),
                                      data["samenvatting"].Value <string>(),
                                      data["duration"].Value <int>(),
                                      filename,
                                      kijkWijzers,
                                      thumbnailName
                                      ));
            } catch (InvalidOperationException except)
            {
                if (except.Message is object && except.Message != "")
                {
                    return(Response.IllegalUpdate(req.Id, except.Message));
                }
                else
                {
                    return(Response.TransactionProtocolViolation(req.Id));
                }
            } catch (ValidationException except)
            {
                return(Response.ValidationError(req.Id, except));
            }
            repository.SaveChangesThenDiscard();
            return(new Response
            {
                status = 200,
                data = req.PostData.ToJson()
            }.ChromelyWrapper(req.Id));
        }