Exemplo n.º 1
0
        public static ResultService <DeleteFileOperations> CreateFileOperations(File file)
        {
            if (file == null)
            {
                return(ResultService <DeleteFileOperations> .Error($"File parametr is null"));
            }


            if (file.IsDeleted)
            {
                ResultService <DeleteFileOperations> .Error($"Can't delete deleted file {file.Key}");
            }

            DeleteFileOperations delete = new DeleteFileOperations();

            delete.KeyUser        = file.KeyUser;
            delete.KeyFile        = file.KeyFile;
            delete.KeyFileVersion = Guid.NewGuid().ToString();
            delete.FileName       = file.FileName;
            delete.Extension      = file.Extension;
            delete.Parent         = null;
            delete.IsAdd          = false;
            delete.IsChange       = false;
            delete.IsClone        = false;
            delete.IsDelete       = true;
            delete.OperationDate  = DateTime.Now;
            delete.CreateOrderBy();
            return(ResultService <DeleteFileOperations> .Ok(delete));
        }
        // [TestMethod]
        public void DeleteFile_RunAddUserAndAddFileAndDeleteFile_AndResultDeleteFile()
        {
            string login    = "******";
            string name     = "Name";
            string surName  = "SurName";
            string password = "******";

            AddUserOperations addUserOperations = AddUserOperations.CreateUserOperations(login, password, name, surName);

            //HiveDatabaseService databaseService = new  HiveDatabaseService(connectionString);
            sut.DoUserOperations(addUserOperations).Wait();;
            User user = sut.GetUser(addUserOperations.KeyUser).Result;


            string            fileName          = "filename";
            string            extension         = "extension";
            AddFileOperations addFileOperations = AddFileOperations.CreateFileOperations(user, fileName, extension).OnError(a => throw new Exception(a)).Value;;

            sut.DoFileOperations(addFileOperations).Wait();;

            File file = sut.GetFile(addFileOperations.KeyUserFile).Result;

            DeleteFileOperations deleteFileOperations = DeleteFileOperations.CreateFileOperations(file).OnError(a => throw new Exception(a)).Value;;


            //Act
            sut.DoFileOperations(deleteFileOperations).Wait();

            // Assert
            File fileRead = sut.GetFile(deleteFileOperations.KeyUserFile).Result;


            Assert.AreEqual(fileName, fileRead.FileName);
            Assert.AreEqual(extension, fileRead.Extension);

            Assert.AreEqual(deleteFileOperations.KeyUser, fileRead.KeyUser);
            Assert.AreEqual(deleteFileOperations.KeyUserFile, fileRead.Key);
            Assert.AreEqual(deleteFileOperations.Key, fileRead.LongKey);
            Assert.AreEqual(deleteFileOperations.OperationDate.ToString("yyyy-MM-dd HH:mm:ss"), file.LastOperationDate.ToString("yyyy-MM-dd HH:mm:ss"));
            Assert.IsTrue(fileRead.IsDeleted);
            Assert.IsFalse(fileRead.IsCloned);
        }
Exemplo n.º 3
0
        public async Task <ResultService <File> > DeleteFileToUser(IFileServiceLogger fileServiceLogger, string userKey, string userFileKey)
        {
            try
            {
                User user = await _databaseService.GetUser(userKey);

                if (user == null)
                {
                    fileServiceLogger.LogDebug(
                        $"Dewey.Dms.FileService.Services.GetInfoFile(userKey={userKey},userFileKey={userFileKey}) - no such user");
                    return(ResultService <File> .Error($"No such user {userKey}"));
                }

                File file = await _databaseService.GetFile(userFileKey);

                if (file == null)
                {
                    fileServiceLogger.LogDebug(
                        $"Dewey.Dms.FileService.Services.GetInfoFile(userKey={userKey},userFileKey={userFileKey}: No such file");
                    return(ResultService <File> .Error("No such file"));
                }

                if (file.KeyUser != userKey)
                {
                    fileServiceLogger.LogDebug(
                        $"Dewey.Dms.FileService.Services.GetInfoFile(userKey={userKey},userFileKey={userFileKey}: Permission denied to file");
                    return(ResultService <File> .Error("No such file"));
                }

                ResultService <DeleteFileOperations> resultDeleteFileOperations =
                    DeleteFileOperations.CreateFileOperations(file);
                if (resultDeleteFileOperations.IsError)
                {
                    fileServiceLogger.LogDebug(
                        $"Dewey.Dms.FileService.Services.GetInfoFile(userKey={userKey},userFileKey={userFileKey}: {resultDeleteFileOperations.ErrorMessage}");
                    return(ResultService <File> .Error(resultDeleteFileOperations.ErrorMessage));
                }

                DeleteFileOperations deleteFileOperations = resultDeleteFileOperations.Value;
                await _databaseService.DoFileOperations(deleteFileOperations);


                File fileDelete = await _databaseService.GetFile(userFileKey);

                if (fileDelete == null)
                {
                    fileServiceLogger.LogDebug(
                        $"Dewey.Dms.FileService.Services.GetInfoFile(userKey={userKey},userFileKey={userFileKey}: Problem with reading deleted file");
                    return(ResultService <File> .Error("Internal Error"));
                }


                return(ResultService <File> .Ok(fileDelete));
            }
            catch (Exception ex)
            {
                fileServiceLogger.LogError(
                    $"Dewey.Dms.FileService.Services.GetInfoFile(userKey={userKey},userFileKey={userFileKey}", ex);
                return(ResultService <File> .Error("Internal server error"));
            }
        }