public async Task <ActionResult <List <FileVersion> > > AddVersions(string resourceName, [FromBody] List <FileVersion> fileVersions)
        {
            if (fileVersions == null)
            {
                return(BadRequest(new { error = "Couldn't parse the versions to add" }));
            }

            var body = await Request.Body.GetStringFromStreamAsync(Encoding.UTF8);

            var errors = CheckVersionsForErrors(fileVersions);

            if (errors != null)
            {
                logger.LogError($"POST/name Couldn't parse body {body}");
                return(BadRequest(errors));
            }

            File f = null;

            await foreach (var item in storageRepository.GetFilesAsync(resourceName))
            {
                // Return an error if there's more than one file with the same name
                if (f != null)
                {
                    logger.LogCritical($"Two files with the same resource name exist : {resourceName}");
                    return(StatusCode(StatusCodes.Status500InternalServerError));
                }
                f = item;
            }

            try
            {
                f.Versions.AddRange(fileVersions);

                if (await storageRepository.UpdateAsync(f.Id, f))
                {
                    return(Created($"/files/{f.ResourceName}/[versions]", f));
                }
                else
                {
                    logger.LogError($"Couldn't add the versions to file {f.Name} with versions {fileVersions}");
                    return(StatusCode(StatusCodes.Status500InternalServerError));
                }
            }
            catch (Exception e)
            {
                logger.LogError($"Error in POST/files : Exception thrown with body  {body} and error {e.Message}");
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }
示例#2
0
 public static async Task DeleteAllFiles(this IStorageRepositoryAsync storageRepo)
 {
     await foreach (var file in storageRepo.GetFilesAsync())
     {
         await storageRepo.DeleteAsync <File>(file.Id);
     }
 }