예제 #1
0
        public void Set(long webResourceId, string accountId)
        {
            if (!WebResourceRepository.Any(r => r.Id == webResourceId))
            {
                throw new ArgumentException("Not exist.", nameof(webResourceId));
            }

            var validAccountId = WebResourceAccountIdValidator.GetValidAccountId(webResourceId, accountId);

            if (validAccountId == null)
            {
                throw new ArgumentException("Invalid.", nameof(accountId));
            }

            var personId = MetaAccountRepository.FirstOrDefault(
                a => a.AccountId == validAccountId && a.WebResourceId == webResourceId,
                a => a.PersonId);

            if (personId == 0)
            {
                var person = new Person();
                PersonService.Save(person);
                UnitOfWork.Commit();

                personId = person.Id;
                var account = new MetaAccount
                {
                    PersonId      = personId,
                    Type          = MetaType.Created,
                    AccountId     = validAccountId,
                    WebResourceId = webResourceId
                };

                MetaAccountService.Save(account);
                UnitOfWork.Commit();
            }

            if (WatchRepository.Any(w => w.PersonId == personId && w.WebResourceId == webResourceId))
            {
                return;
            }

            Save(new Watch
            {
                PersonId      = personId,
                WebResourceId = webResourceId
            });
        }