public void Validate(CreateStorageItemDto storageItem)
        {
            Console.WriteLine(storageItem);
            //Request Validation
            if (storageItem == null)
            {
                throw new BadRequestCustomException("Request body cannot be empty");
            }
            if (storageItem.IsFolder && storageItem.Data != null)
            {
                throw new BadRequestCustomException("Folder cannot contain data");
            }

            if (!storageItem.IsFolder && storageItem.Data == null)
            {
                throw new BadRequestCustomException("File must contain data");
            }

            if (storageItem.Name == null || storageItem.Name.Length == 0)
            {
                throw new BadRequestCustomException("Item must have a name");
            }

            //Operation Validation
            if (NameExistsInFolder(storageItem.Name, storageItem.FolderId))
            {
                throw new NameTakenException(storageItem.Name);
            }

            if (!ItemIsFolder(storageItem.FolderId))
            {
                throw new BadRequestCustomException($"Id {storageItem.FolderId} is not a folder");
            }
        }
Example #2
0
        public StorageItem CreateFile(CreateStorageItemDto item)
        {
            var newItem = new StorageItem();

            newItem.Name     = item.Name;
            newItem.FolderId = item.FolderId;
            newItem.IsFolder = item.IsFolder;
            newItem.Created  = DateTime.Now;
            newItem.IsTrash  = false;
            newItem.Size     = item.Size;
            newItem.Type     = item.Type;
            newItem.Guid     = Guid.NewGuid().ToString();
            if (!newItem.IsFolder)
            {
                Files.WriteToFile(newItem.Guid, item.Data);
            }

            return(newItem);
        }