コード例 #1
0
        public async Task <IActionResult> GetInstagramMedia(
            CancellationToken cancellationToken,
            [FromRoute] string login,
            [FromServices] Domain.Instapage.IInstapageRepository instapageRepository,
            [FromServices] Infrastructure.Instagram.InstapageCreationService creationService,
            [FromQuery] int limit = 16)
        {
            var instapage = await instapageRepository.GetByInstagram(login, cancellationToken);

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

            if (instapage.UserId != User.GetId())
            {
                return(NotFound());
            }

            try
            {
                var media = await creationService.GetMedia(instapage.AccessToken, limit, cancellationToken);

                return(Ok(media));
            }
            catch (Infrastructure.Instagram.ApiException ex)
            {
                return(BadRequest(new ErrorView(ErrorCode.InstagramException, ex.Reason)));
            }
        }
コード例 #2
0
        public async Task <IActionResult> DisableInstapage(
            CancellationToken cancellationToken,
            [FromRoute] string login,
            [FromServices] Domain.Instapage.IInstapageRepository instapageRepository)
        {
            var instapage = await instapageRepository.GetByInstagram(login, cancellationToken);

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

            if (instapage.UserId != User.GetId())
            {
                return(NotFound());
            }
            try
            {
                instapage.Disable();
            }
            catch (InvalidOperationException ex)
            {
                return(UnprocessableEntity(ex.Message));
            }

            await instapageRepository.Save(instapage);

            return(NoContent());
        }
コード例 #3
0
        public async Task <IActionResult> FbAuthentication(
            CancellationToken cancellationToken,
            [FromForm] FbAuthenticationBinding fbBinding,
            [FromServices] Infrastructure.Instagram.InstapageCreationService creationService,
            [FromServices] Domain.Instapage.IInstapageRepository instapageRepository)
        {
            try
            {
                var instapage = await creationService.CreateInstapage(User.GetId(), fbBinding.Code, cancellationToken);

                var instapageExists = await instapageRepository.GetByInstagram(instapage.InstagramLogin, cancellationToken);

                if (instapageExists != null)
                {
                    return(Conflict());
                }

                await instapageRepository.Save(instapage);

                return(Ok(new InstapageView
                {
                    Login = instapage.InstagramLogin
                }));
            }
            catch (Infrastructure.Instagram.ApiException ex)
            {
                return(BadRequest(new ErrorView(ErrorCode.InstagramException, ex.Reason)));
            }
        }
コード例 #4
0
        public async Task <IActionResult> CreateInstapage(
            CancellationToken cancellationToken,
            [FromRoute] string login,
            [FromBody] CreateInstapageBinding binding,
            [FromServices] Domain.Instapage.IInstapageRepository instapageRepository,
            [FromServices] Infrastructure.Instagram.InstapageCreationService creationService)
        {
            var instapage = await instapageRepository.GetByInstagram(login, cancellationToken);

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

            if (instapage.UserId != User.GetId())
            {
                return(NotFound());
            }

            instapage.SetInfo(binding.Title, binding.Description, binding.Vkontakte, binding.Telegram);

            try
            {
                foreach (var instapost in binding.Posts)
                {
                    if (instapage.Instaposts.Any(o => o.Id == instapost.Id))
                    {
                        continue;
                    }

                    await creationService.SaveMedia(instapage.AccessToken, instapage.InstagramLogin, instapost.Id, cancellationToken);

                    instapage.AddPost(new Domain.Instapage.Instapost(
                                          id: instapost.Id,
                                          instapageId: instapage.Id,
                                          title: instapost.Title,
                                          description: instapost.Description,
                                          price: instapost.Price,
                                          currency: instapost.Currency,
                                          timestamp: instapost.Timestamp));
                }

                await instapageRepository.Save(instapage);

                return(NoContent());
            }
            catch (Infrastructure.Instagram.ApiException ex)
            {
                return(BadRequest(new ErrorView(ErrorCode.InstagramException, ex.Reason)));
            }
        }
コード例 #5
0
        public async Task <IActionResult> GetImage(
            CancellationToken cancellationToken,
            [FromRoute] string login,
            [FromQuery] string id,
            [FromServices] Domain.Instapage.IInstapageRepository instapageRepository,
            [FromServices] Domain.IFileStorage fileStorage)
        {
            var instapage = await instapageRepository.GetByInstagram(login, cancellationToken);

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

            // Feature Work In Progress [WIP]
            //if (instapage.State != Domain.Instapage.InstapageState.Enabled)
            //    return BadRequest();

            try
            {
                (var fs, var extension) = fileStorage.GetFileWithoutExtension(id, login);

                switch (extension)
                {
                case ".png":
                    return(File(fs, "image/png"));

                case ".jpg":
                case ".jpeg":
                    return(File(fs, "image/jpeg"));

                case ".ico":
                    return(File(fs, "image/x-icon"));

                default:
                    return(UnprocessableEntity());
                }
            }
            catch
            {
                return(NotFound());
            }
        }
コード例 #6
0
        public async Task <IActionResult> UploadImages(
            CancellationToken cancellationToken,
            [FromRoute] string login,
            [FromForm] UploadImagesBinding binding,
            [FromServices] Domain.Instapage.IInstapageRepository instapageRepository,
            [FromServices] Infrastructure.Instagram.InstapageCreationService creationService)
        {
            var instapage = await instapageRepository.GetByInstagram(login, cancellationToken);

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

            if (instapage.UserId != User.GetId())
            {
                return(NotFound());
            }

            if (!binding.Background.IsImage() || !binding.Favicon.IsImage())
            {
                return(BadRequest());
            }

            var bgExtension = Path.GetExtension(binding.Background.FileName);

            using var bgStream = binding.Background.OpenReadStream();
            await creationService.UploadBackground(bgExtension, bgStream, login, cancellationToken);

            var faviconExtension = Path.GetExtension(binding.Favicon.FileName);

            using var faviconStream = binding.Favicon.OpenReadStream();
            await creationService.UploadFavicon(faviconExtension, faviconStream, login, cancellationToken);

            return(NoContent());
        }