Пример #1
0
        public TaskResult <Nothing> DoTask(ArtistAccount update)
        {
            try
            {
                var artistAccount = _dbContext.ArtistAccounts.SingleOrDefault(aa => aa.Id == update.Id);

                if (artistAccount == null)
                {
                    throw new TaskException(SystemMessage("ARTIST_ACCOUNT_NOT_FOUND"));
                }

                if (update.IsPreferred)
                {
                    var allArtistAccounts = _dbContext.ArtistAccounts.ToList();
                    foreach (var account in allArtistAccounts)
                    {
                        account.IsPreferred = false;
                    }

                    _dbContext.SaveChanges();
                }

                artistAccount.PlatformId  = update.Platform?.Id ?? update.PlatformId;
                artistAccount.Platform    = _dbContext.Platforms.Single(p => p.Id == artistAccount.PlatformId);
                artistAccount.IsPreferred = update.IsPreferred;
                artistAccount.Username    = update.Username;
                _dbContext.SaveChanges();

                return(new TaskResult <Nothing>(true));
            }
            catch (Exception e)
            {
                return(new TaskResult <Nothing>(new TaskException(e)));
            }
        }
        public TaskResult <int?> DoTask(ArtistAccount artistAccount)
        {
            try
            {
                if (artistAccount.IsPreferred)
                {
                    var allArtistAccounts = _dbContext.ArtistAccounts.ToList();
                    foreach (var account in allArtistAccounts)
                    {
                        account.IsPreferred = false;
                    }

                    _dbContext.SaveChanges();
                }

                var artistId   = artistAccount.Artist?.Id ?? artistAccount.ArtistId;
                var platformId = artistAccount.Platform?.Id ?? artistAccount.PlatformId;

                artistAccount.Artist     = null;
                artistAccount.ArtistId   = artistId;
                artistAccount.Platform   = null;
                artistAccount.PlatformId = platformId;

                _dbContext.ArtistAccounts.Add(artistAccount);
                _dbContext.SaveChanges();

                return(new TaskResult <int?>(artistAccount.Id));
            }
            catch (Exception e)
            {
                return(new TaskResult <int?>(new TaskException(e)));
            }
        }
        public IActionResult UpdateArtistAccount(int artistId, int artistAccountId, ArtistAccount artistAccount)
        {
            try
            {
                if (!ClientKeyIsValid())
                {
                    return(Unauthorized());
                }

                if (!UserIsAuthenticatedAndAuthorized(MethodBase.GetCurrentMethod()))
                {
                    return(Unauthorized());
                }

                if (!UserIsAuthorizedForArtist(artistId))
                {
                    return(Unauthorized());
                }

                var invalidArtistPathResult = InvalidArtistPathResult(artistId);
                if (invalidArtistPathResult != null)
                {
                    return(invalidArtistPathResult);
                }

                var getArtistAccountResult = _getArtistAccountTask.DoTask(artistAccountId);

                if (getArtistAccountResult.HasException)
                {
                    return(Error(getArtistAccountResult.Exception));
                }

                if (getArtistAccountResult.HasNoData)
                {
                    return(NotFound());
                }

                if (getArtistAccountResult.Data.ArtistId != artistId)
                {
                    return(BadRequest());
                }

                artistAccount.ArtistId = artistId;
                artistAccount.Id       = artistAccountId;
                var taskResults = _updateArtistAccountTask.DoTask(artistAccount);

                return(taskResults.Success ?
                       Ok() :
                       Error(taskResults.Exception));
            }
            catch (Exception e)
            {
                return(Error(e));
            }
        }
        public TaskResult <bool> DoTask(ArtistAccount artistAccount)
        {
            try
            {
                var toRemove = _dbContext.ArtistAccounts.SingleOrDefault(aa => aa.Id == artistAccount.Id);
                if (toRemove == null)
                {
                    return(new TaskResult <bool>(false));
                }

                _dbContext.ArtistAccounts.Remove(toRemove);
                _dbContext.SaveChanges();

                return(new TaskResult <bool>(true));
            }
            catch (Exception e)
            {
                return(new TaskResult <bool>(new TaskException(e)));
            }
        }
        public IActionResult AddArtistAccount(int artistId, ArtistAccount artistAccount)
        {
            try
            {
                if (!ClientKeyIsValid())
                {
                    return(Unauthorized());
                }

                if (!UserIsAuthenticatedAndAuthorized(MethodBase.GetCurrentMethod()))
                {
                    return(Unauthorized());
                }

                if (!UserIsAuthorizedForArtist(artistId))
                {
                    return(Unauthorized());
                }

                var invalidArtistPathResult = InvalidArtistPathResult(artistId);
                if (invalidArtistPathResult != null)
                {
                    return(invalidArtistPathResult);
                }

                artistAccount.ArtistId = artistId;
                var taskResults = _addArtistAccountTask.DoTask(artistAccount);

                return(taskResults.Success ?
                       Json(taskResults) :
                       Error(taskResults.Exception));
            }
            catch (Exception e)
            {
                return(Error(e));
            }
        }
        public void TaskSuccessTest()
        {
            var testArtist      = TestsModel.Artist;
            var addArtistTask   = new AddArtist(DbContext, new FormattingService());
            var addArtistResult = addArtistTask.DoTask(testArtist);

            Assert.IsTrue(addArtistResult.Success);
            Assert.IsNull(addArtistResult.Exception);

            var paymentService = new ListServices(DbContext).DoTask(null).Data.SingleOrDefault(s => s.Name.ToLower() == "payment");

            Assert.IsNotNull(paymentService);

            var allPlatforms     = new ListPlatforms(DbContext).DoTask(null).Data.ToList();
            var paymentPlatforms = new List <Platform>();

            foreach (var platform in allPlatforms)
            {
                paymentPlatforms.AddRange(from service in platform.Services where service.Id == paymentService.Id select platform);
            }

            var paymentPlatform = paymentPlatforms[new Random().Next(0, paymentPlatforms.Count)];

            var artistAccount = new ArtistAccount
            {
                IsPreferred = true,
                Platform    = paymentPlatform,
                Artist      = testArtist,
                Username    = "******" + DateTime.Now.Ticks
            };

            var addArtistAccountTask   = new AddArtistAccount(DbContext);
            var addArtistAccountResult = addArtistAccountTask.DoTask(artistAccount);

            Assert.IsTrue(addArtistAccountResult.Success);
            Assert.IsNull(addArtistAccountResult.Exception);
            Assert.IsNotNull(addArtistAccountResult.Data);

            var getArtistAccountTask   = new GetArtistAccount(DbContext);
            var getArtistAccountResult = getArtistAccountTask.DoTask(artistAccount.Id);

            Assert.IsTrue(getArtistAccountResult.Success);
            Assert.IsNull(getArtistAccountResult.Exception);
            Assert.IsNotNull(getArtistAccountResult.Data);

            Assert.AreEqual(artistAccount.PlatformId, getArtistAccountResult.Data.PlatformId);
            Assert.AreEqual(artistAccount.ArtistId, getArtistAccountResult.Data.ArtistId);
            Assert.AreEqual(artistAccount.IsPreferred, getArtistAccountResult.Data.IsPreferred);
            Assert.AreEqual(artistAccount.Username, getArtistAccountResult.Data.Username);

            artistAccount.Username    = "******" + DateTime.Now.Ticks;
            artistAccount.IsPreferred = false;

            var task   = new UpdateArtistAccount(DbContext);
            var result = task.DoTask(artistAccount);

            Assert.IsTrue(result.Success);
            Assert.IsNull(result.Exception);

            getArtistAccountTask   = new GetArtistAccount(DbContext);
            getArtistAccountResult = getArtistAccountTask.DoTask(artistAccount.Id);
            Assert.AreEqual(artistAccount.PlatformId, getArtistAccountResult.Data.PlatformId);
            Assert.AreEqual(artistAccount.ArtistId, getArtistAccountResult.Data.ArtistId);
            Assert.AreEqual(artistAccount.IsPreferred, getArtistAccountResult.Data.IsPreferred);
            Assert.AreEqual(artistAccount.Username, getArtistAccountResult.Data.Username);

            var removeArtistTask   = new RemoveArtist(DbContext);
            var removeArtistResult = removeArtistTask.DoTask(testArtist);

            Assert.IsTrue(removeArtistResult.Success);
            Assert.IsNull(removeArtistResult.Exception);
        }
        public void TaskSuccessTest()
        {
            var addArtistTask   = new AddArtist(DbContext, new FormattingService());
            var testArtist      = TestsModel.Artist;
            var addArtistResult = addArtistTask.DoTask(testArtist);

            Assert.IsTrue(addArtistResult.Success);
            Assert.IsNull(addArtistResult.Exception);
            Assert.IsNotNull(addArtistResult.Data);

            var artistId = addArtistResult.Data;

            Assert.IsNotNull(artistId);
            Assert.IsTrue(artistId > 0);

            var paymentService = new ListServices(DbContext).DoTask(null).Data.SingleOrDefault(s => s.Name.ToLower() == "payment");

            Assert.IsNotNull(paymentService);

            var allPlatforms     = new ListPlatforms(DbContext).DoTask(null).Data.ToList();
            var paymentPlatforms = new List <Platform>();

            foreach (var platform in allPlatforms)
            {
                paymentPlatforms.AddRange(from service in platform.Services where service.Id == paymentService.Id select platform);
            }

            foreach (var paymentPlatform in paymentPlatforms)
            {
                var artistAccount = new ArtistAccount
                {
                    IsPreferred = new Random().Next(0, 2) == 0,
                    Platform    = paymentPlatform,
                    Artist      = testArtist,
                    Username    = "******" + new Random().Next(100, 999)
                };
                var addArtistAccountTask   = new AddArtistAccount(DbContext);
                var addArtistAccountResult = addArtistAccountTask.DoTask(artistAccount);

                Assert.IsTrue(addArtistAccountResult.Success);
                Assert.IsNull(addArtistAccountResult.Exception);
                Assert.IsNotNull(addArtistAccountResult.Data);
            }

            var task   = new ListArtistAccounts(DbContext);
            var result = task.DoTask(testArtist);

            Assert.IsTrue(result.Success);
            Assert.IsNull(result.Exception);
            Assert.IsNotNull(result.Data);
            Assert.AreEqual(paymentPlatforms.Count, result.Data.Count);

            foreach (var userAccount in result.Data)
            {
                Assert.AreEqual(userAccount.ArtistId, testArtist.Id);
                Assert.IsNotNull(userAccount.Username);
            }

            var removeArtistTask   = new RemoveArtist(DbContext);
            var removeArtistResult = removeArtistTask.DoTask(testArtist);

            Assert.IsTrue(removeArtistResult.Success);
            Assert.IsNull(removeArtistResult.Exception);
        }