Beispiel #1
0
        /// <summary>
        /// Ensures that the request is valid.
        /// </summary>
        private async Task ValidateAsync(SharedLinkVM vm)
        {
            if (!string.IsNullOrEmpty(vm.From) && vm.From.TryParse <DateTime?>() == null)
            {
                throw new OperationException($"Date '{vm.From}' is not valid.");
            }

            if (!string.IsNullOrEmpty(vm.To) && vm.To.TryParse <DateTime?>() == null)
            {
                throw new OperationException($"Date '{vm.To}' is not valid.");
            }

            if (!Enum.IsDefined(typeof(SearchScope), vm.Scope))
            {
                throw new OperationException($"Search mode '{vm.Scope}' is not supported.");
            }

            if (vm.Tags?.Length > 0)
            {
                var existingCount = await _db.Tags.CountAsync(x => vm.Tags.Contains(x.Id));

                if (existingCount != vm.Tags.Length)
                {
                    throw new OperationException($"Some tags of the specified list '{vm.Tags}' do not exist!");
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Creates a new shared link.
        /// </summary>
        public async Task <KeyResultVM> CreateAsync(SharedLinkVM vm)
        {
            await ValidateAsync(vm);

            var folderPath = PathHelper.Normalize(vm.Folder);
            var folder     = await _db.Folders.GetAsync(x => x.Path == folderPath, $"Folder '{vm.Folder}' does not exist.");

            var link = new SharedLink
            {
                Key          = UniqueKey.Get(),
                CreationDate = DateTime.Now,
                Folder       = folder,
            };

            _mapper.Map(vm, link);

            _db.SharedLinks.Add(link);
            await _db.SaveChangesAsync();

            return(new KeyResultVM {
                Key = link.Key
            });
        }
 public Task <KeyResultVM> Create([FromBody] SharedLinkVM vm)
 {
     return(_smMgr.CreateAsync(vm));
 }