Beispiel #1
0
        public TaskResult <int?> DoTask(User user)
        {
            try
            {
                var proId         = user.PerformingRightsOrganization?.Id ?? user.PerformingRightsOrganizationId;
                var publisherId   = user.Publisher?.Id ?? user.PublisherId;
                var recordLabelId = user.RecordLabel?.Id ?? user.RecordLabelId;

                user.PerformingRightsOrganization   = null;
                user.PerformingRightsOrganizationId = proId;
                user.Publisher     = null;
                user.PublisherId   = publisherId;
                user.RecordLabel   = null;
                user.RecordLabelId = recordLabelId;

                var person = user.Person;
                if (person != null)
                {
                    person.Email = user.AuthenticationId;
                    var addPersonResult = _addPersonTask.DoTask(person);
                    if (!addPersonResult.Success)
                    {
                        throw addPersonResult.Exception;
                    }

                    var personId = addPersonResult.Data;
                    user.Person   = null;
                    user.PersonId = personId;
                }

                user.SocialSecurityNumber = _formattingService.FormatSocialSecurityNumber(user.SocialSecurityNumber);
                user.PerformingRightsOrganizationMemberNumber = string.IsNullOrWhiteSpace(user.PerformingRightsOrganizationMemberNumber) ? null : user.PerformingRightsOrganizationMemberNumber;
                user.SoundExchangeAccountNumber = string.IsNullOrWhiteSpace(user.SoundExchangeAccountNumber) ? null : user.SoundExchangeAccountNumber;

                _dbContext.Users.Add(user);
                _dbContext.SaveChanges();

                user.Person = person;
                user.PerformingRightsOrganization = proId > 0 ?
                                                    _dbContext.PerformingRightsOrganizations.Where(p => p.Id == proId)
                                                    .Include(p => p.Country)
                                                    .SingleOrDefault() : null;
                user.Publisher = publisherId > 0 ?
                                 _dbContext.Publishers.Where(p => p.Id == publisherId)
                                 .Include(p => p.Address).ThenInclude(a => a.Country)
                                 .SingleOrDefault() : null;
                user.RecordLabel = recordLabelId > 0 ?
                                   _dbContext.RecordLabels.Where(l => l.Id == recordLabelId)
                                   .Include(p => p.Address).ThenInclude(a => a.Country)
                                   .SingleOrDefault() : null;

                return(new TaskResult <int?>(user.Id));
            }
            catch (Exception e)
            {
                return(new TaskResult <int?>(new TaskException(e)));
            }
        }
        public TaskResult <Nothing> DoTask(User update)
        {
            try
            {
                var user = _dbContext.Users.Where(u => u.Id == update.Id)
                           .Include(u => u.Person)
                           .SingleOrDefault();

                if (user == null)
                {
                    throw new TaskException(SystemMessage("USER_NOT_FOUND"));
                }

                if (update.Person != null)
                {
                    update.Person.Email = update.AuthenticationId;
                    if (user.Person == null)
                    {
                        _addPersonTask.DoTask(update.Person);
                    }
                    else
                    {
                        update.Person.Id = user.Person.Id;
                        _updatePersonTask.DoTask(update.Person);
                    }
                }

                user.PerformingRightsOrganizationId = update.PerformingRightsOrganization?.Id;
                if (user.PerformingRightsOrganizationId.HasValue)
                {
                    var pro = _dbContext.PerformingRightsOrganizations.SingleOrDefault(r => r.Id == user.PerformingRightsOrganizationId);
                    user.PerformingRightsOrganization = pro ?? throw new TaskException(SystemMessage("PRO_NOT_FOUND"));
                }

                user.PublisherId = update.Publisher?.Id;
                if (user.PublisherId.HasValue)
                {
                    var publisher = _dbContext.Publishers.SingleOrDefault(p => p.Id == user.PublisherId);
                    user.Publisher = publisher ?? throw new TaskException(SystemMessage("PUBLISHER_NOT_FOUND"));
                }

                user.RecordLabelId = update.RecordLabel?.Id;
                if (user.RecordLabelId.HasValue)
                {
                    var recordLabel = _dbContext.RecordLabels.SingleOrDefault(p => p.Id == user.RecordLabelId);
                    user.RecordLabel = recordLabel ?? throw new TaskException(SystemMessage("RECORD_LABEL_NOT_FOUND"));
                }

                user.AuthenticationId = update.AuthenticationId;
                user.Type             = update.Type;
                user.Roles            = update.Roles;
                user.PerformingRightsOrganizationMemberNumber = string.IsNullOrWhiteSpace(update.PerformingRightsOrganizationMemberNumber) ? null : update.PerformingRightsOrganizationMemberNumber;
                user.SoundExchangeAccountNumber = string.IsNullOrWhiteSpace(update.SoundExchangeAccountNumber) ? null : update.SoundExchangeAccountNumber;
                user.SocialSecurityNumber       = _formattingService.FormatSocialSecurityNumber(update.SocialSecurityNumber);

                _dbContext.SaveChanges();

                return(new TaskResult <Nothing>(true));
            }
            catch (Exception e)
            {
                return(new TaskResult <Nothing>(new TaskException(e)));
            }
        }