public async Task <MiniTag> Handle(MiniTagSubmissionRequest request, CancellationToken cancellationToken)
        {
            Tag newTag = new Tag();

            //MiniTags can be created by passing a tagName or an ID.
            if (!String.IsNullOrEmpty(request.Tag.TagName))
            {
                newTag = await AddAndFindTagByName(request.Tag.TagName);
            }
            else
            {
                //Don't need to make a new tag, select the existing tag
                newTag = _context.Tag.Where(t => t.ID == request.Tag.ID)
                         .Include(m => m.MiniTags)
                         .First();
            }

            Mini newMini = _context.Mini.Where(m => m.ID == request.Mini.ID)
                           .Include(m => m.Creator)
                           .Include(m => m.MiniTags)
                           .ThenInclude(mt => mt.Tag)
                           .First();

            MiniTag ret = await AddMiniTag(newMini, newTag, request.User);

            //TODO - If this breaks because the tag structure is too complicated serialize the adding of MiniTags by moving the SaveChanges into the function and await each AddMiniTag.
            foreach (Tag pairedTag in FindPairedTags(newTag))
            {
                AddMiniTag(newMini, pairedTag, request.User);
            }
            await _context.SaveChangesAsync();

            //TODO: Change to created
            return(ret);
        }
Example #2
0
        public async Task <IActionResult> Patch([FromBody] Tag value)
        {
            Tag Tag = await _context.Tag.FindAsync(value.ID);

            if (Tag == null)
            {
                return(NotFound());
            }

            if (value.Category != null)
            {
                Tag.Category = value.Category;
            }

            if (value.TagName != null)
            {
                Tag.TagName = value.TagName;
            }

            _context.Attach(Tag).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                return(Conflict());
            }

            return(Ok("{}"));
        }
Example #3
0
        public async Task <IActionResult> Post(int id)
        {
            Starred newStarred = new Starred
            {
                Mini = await _context.Mini.FindAsync(id),
                User = await _userManager.GetUserAsync(User)
            };

            await _context.Starred.AddAsync(newStarred);

            await _context.SaveChangesAsync();

            return(Ok("{}"));
        }
Example #4
0
        public async Task <IActionResult> Patch([FromBody] MiniTag value)
        {
            MiniTag MiniTag = await _context.MiniTag.FindAsync(value.MiniID, value.TagID);

            if (MiniTag == null)
            {
                return(NotFound());
            }

            MiniTag.Status                 = value.Status;
            MiniTag.LastModifiedTime       = DateTime.Now;
            _context.Attach(MiniTag).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                return(Conflict());
            }

            return(Ok("{}"));
        }
        public async Task <IActionResult> Patch([FromBody] Mini value)
        {
            Mini Mini = await _context.Mini
                        .Include(m => m.MiniTags)
                        .FirstOrDefaultAsync(m => m.ID == value.ID);

            if (Mini == null)
            {
                return(NotFound());
            }

            if (value.Status == Status.Approved)
            {
                Mini.Status       = Status.Approved;
                Mini.ApprovedTime = DateTime.Now;
                foreach (MiniTag mt in Mini.MiniTags)
                {
                    if (mt.Status == Status.Pending)
                    {
                        mt.Status                 = Status.Approved;
                        mt.ApprovedTime           = DateTime.Now;
                        mt.LastModifiedTime       = DateTime.Now;
                        _context.Attach(mt).State = EntityState.Modified;
                    }
                }
            }
            else
            {
                Mini.Status = value.Status;
            }

            _context.Attach(Mini).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                throw;
            }

            return(Ok(Mini.ID));
        }
        public async Task <Mini> Handle(MiniSubmissionRequest request, CancellationToken cancellationToken)
        {
            //TODO - This should look at MiniSourceSite, not m.Link.
            Mini mini = await _context.Mini.TagWith("MiniSubmissionHandler.cs 1").FirstOrDefaultAsync(m => m.Link == request.Url.ToString(), cancellationToken);

            IParser parser = _parsers.FirstOrDefault(p => p.CanParse(request.Url));

            if (parser is null)
            {
                //valid URL, but not currently supported
                //TODO: log when this happens?
                return(null);
            }

            if (mini != null)
            {
                if (request.JustThumbnail)
                {
                    Mini updatedMini = await parser.ParseFromUrl(request.Url);

                    mini.Thumbnail = updatedMini.Thumbnail;
                    _context.Attach(mini).State = EntityState.Modified;
                    await UploadThumbnail(mini);
                }
                return(mini);
            }

            mini = await parser.ParseFromUrl(request.Url);

            //TODO - This should look at MiniSourceSite, not m.Link.
            //Now that we've parsed it, check if the parsed URL is different from the original URL and if we have that.
            Mini checkDupe = await _context.Mini.FirstOrDefaultAsync(m => m.Link == mini.Link, cancellationToken);

            if (checkDupe != null)
            {
                if (request.JustThumbnail)
                {
                    checkDupe.Thumbnail         = mini.Thumbnail;
                    _context.Attach(mini).State = EntityState.Modified;
                    await UploadThumbnail(checkDupe);
                }
                return(checkDupe);
            }

            if (!request.JustThumbnail)
            {
                mini.User   = request.User;
                mini.Status = Status.Unindexed;

                _context.Add(mini);

                await CorrectMiniCreator(mini, cancellationToken);

                //TODO - Another dupe check here based on name and creator

                await _context.SaveChangesAsync();
            }

            await UploadThumbnail(mini);

            return(mini);
        }