public User Create(User user, SUGARContext context = null)
        {
            var didCreate = false;

            if (context == null)
            {
                context   = ContextFactory.Create();
                didCreate = true;
            }

            if (context.Users.Any(g => g.Name == user.Name))
            {
                throw new DuplicateRecordException($"A user with the name: \"{user.Name}\" already exists.");
            }

            context.Users.Add(user);

            if (didCreate)
            {
                context.SaveChanges();
                context.Dispose();
            }

            return(user);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Immediately creates a new relationship between 2 actors
        /// </summary>
        /// <param name="newRelation">Relationship to create</param>
        /// <param name="context">Optional DbContext to perform opperations on. If ommitted a DbContext will be created.</param>
        public void CreateRelationship(ActorRelationship newRelation, SUGARContext context = null)
        {
            var didCreateContext = false;

            if (context == null)
            {
                context          = ContextFactory.Create();
                didCreateContext = true;
            }

            if (newRelation.AcceptorId == newRelation.RequestorId)
            {
                throw new InvalidRelationshipException("Two different users are needed to create a relationship.");
            }

            var hasConflicts = context.Relationships
                               .Any(r => AreRelated(r, newRelation));

            if (!hasConflicts)
            {
                hasConflicts = context.RelationshipRequests
                               .Any(r => AreRelated(r, newRelation));
            }

            if (hasConflicts)
            {
                throw new DuplicateRelationshipException("A relationship with these users already exists.");
            }

            var requestorExists = context.Actors.Any(u => u.Id == newRelation.RequestorId);

            if (!requestorExists)
            {
                throw new InvalidRelationshipException("The requesting user does not exist.");
            }

            var acceptorExists = context.Actors.Any(u => u.Id == newRelation.AcceptorId);

            if (!acceptorExists)
            {
                throw new InvalidRelationshipException("The targeted user does not exist.");
            }

            var relation = new ActorRelationship
            {
                RequestorId = newRelation.RequestorId,
                AcceptorId  = newRelation.AcceptorId
            };

            context.Relationships.Add(relation);

            if (didCreateContext)
            {
                context.SaveChanges();
                context.Dispose();
            }
        }
        public Role GetDefault(ClaimScope scope, SUGARContext context = null)
        {
            var didCreate = false;

            if (context == null)
            {
                context   = ContextFactory.Create();
                didCreate = true;
            }

            var role = context.Roles.FirstOrDefault(r => r.ClaimScope == scope && r.Default);

            if (didCreate)
            {
                context.Dispose();
            }

            return(role);
        }
Exemplo n.º 4
0
        public ActorRole Create(ActorRole actorRole, SUGARContext context = null)
        {
            var didCreate = false;

            if (context == null)
            {
                context   = ContextFactory.Create();
                didCreate = true;
            }

            context.ActorRoles.Add(actorRole);

            if (didCreate)
            {
                context.SaveChanges();
                context.Dispose();
            }

            return(actorRole);
        }
Exemplo n.º 5
0
        public Game Create(Game game, SUGARContext context = null)
        {
            var didCreateContext = false;

            if (context == null)
            {
                context          = ContextFactory.Create();
                didCreateContext = true;
            }

            context.Games.Add(game);

            if (didCreateContext)
            {
                context.SaveChanges();
                context.Dispose();
            }

            return(game);
        }
Exemplo n.º 6
0
        public Group Create(Group group, SUGARContext context = null)
        {
            var didCreateContext = false;

            if (context == null)
            {
                context          = ContextFactory.Create();
                didCreateContext = true;
            }

            context.Groups.Add(group);

            if (didCreateContext)
            {
                context.SaveChanges();
                context.Dispose();
            }

            return(group);
        }