コード例 #1
0
ファイル: LinkService.cs プロジェクト: grexican/isnsfw
        public object Post(CreateLinkRequest request)
        {
            var link = request.ConvertTo <Link>();

            link.SessionId = Session.Id;
            link.CreatedAt = DateTime.UtcNow;

            if (link.Key.IsNullOrEmpty())
            {
                var currentIterator = StartKeyLength * 10;
                do
                {
                    ++currentIterator;
                    link.Key = KeyValidator.GenerateKey(currentIterator / 10);
                } while (_linkRepo.KeyExists(link.Key));
            }

            UnitOfWork(() =>
            {
                _linkRepo.Create(link);

                if (request.Tags != null)
                {
                    link.LinkTags = new List <LinkTag>();

                    var tagsDict = _tagRepo.GetTagsDictionary();

                    foreach (var tag in request.Tags)
                    {
                        if (!tagsDict.ContainsKey(tag))
                        {
                            throw HttpError.NotFound($"Tag '{tag}' not found.");
                        }

                        var linkTag = new LinkTag()
                        {
                            LinkId = link.Id,
                            TagId  = tagsDict[tag].Id
                        };

                        link.LinkTags.Add(linkTag);
                    }

                    _linkRepo.SetLinkTags(link.Id, link.LinkTags);
                }

                PublishMessage(new CreateLinkScreenshotRequest()
                {
                    Id = link.Id
                });
            });

            return(_linkRepo.GetLinkResponse(link.Id));
        }