示例#1
0
        public string Execute(params string[] args)
        {
            Check.CheckLength(2, args);

            var username = args[0];
            var password = args[1];

            using (var context = new TeamBuilderDbContext())
            {
                var isUserExist = context.Users.Any(u => u.Username == username && u.Password == password && u.IsDeleted == false);

                if (!isUserExist)
                {
                    throw new ArgumentException(Constants.ErrorMessages.UserOrPasswordIsInvalid);
                }

                if (Session.User != null)
                {
                    throw new InvalidOperationException(Constants.ErrorMessages.LogoutFirst);
                }

                var user = context.Users.SingleOrDefault(u => u.Username == username && u.Password == password);

                Session.User = user;
            }

            return($"User {username} successfully logged in!");
        }
 public static bool IsUserExisting(string username)
 {
     using (TeamBuilderDbContext context = new TeamBuilderDbContext())
     {
         return(context.Users.Any(u => u.Username == username));
     }
 }
 public static bool IsTeamExisting(string teamName)
 {
     using (TeamBuilderDbContext context = new TeamBuilderDbContext())
     {
         return(context.Teams.Any(t => t.Name == teamName));
     }
 }
 public static bool IsInviteExisting(string teamName, User user)
 {
     using (TeamBuilderDbContext context = new TeamBuilderDbContext())
     {
         return(context.Invitations
                .Any(i => i.Team.Name == teamName && i.InvitedUserId == user.Id && i.IsActive));
     }
 }
 public static bool IsEventExisting(string eventName)
 {
     using (TeamBuilderDbContext context = new TeamBuilderDbContext())
     {
         return(context.Events
                .Any(e => e.Name == eventName));
     }
 }
 internal static void ResetDb()
 {
     using (var context = new TeamBuilderDbContext())
     {
         context.Database.EnsureDeleted();
         context.Database.EnsureCreated();
     }
 }
示例#7
0
        public string Execute(params string[] args)
        {
            Check.CheckLength(2, args);

            var teamName = args[0];
            var username = args[1];

            using (var context = new TeamBuilderDbContext())
            {
                var team = context.Teams.SingleOrDefault(t => t.Name == teamName);
                var user = context.Users.SingleOrDefault(u => u.Username == username);

                if (team == null || user == null)
                {
                    throw new ArgumentException(Constants.ErrorMessages.TeamOrUserNotExist);
                }

                if (Session.User == null)
                {
                    throw new InvalidOperationException(Constants.ErrorMessages.LoginFirst);
                }

                var isCreator       = CommandHelper.IsUserCreatorOfTeam(teamName, user);
                var isAlreadyInTeam = CommandHelper.IsMemberOfTeam(teamName, username);

                if (isCreator)
                {
                    var userTeam = new UserTeam()
                    {
                        Team = team,
                        User = user
                    };

                    user.CreatedUserTeams.Add(userTeam);
                    team.Members.Add(userTeam);
                    context.UserTeams.Add(userTeam);
                    context.SaveChanges();

                    return($"Team {teamName} invited {username}!");
                }

                if (isCreator || isAlreadyInTeam)
                {
                    throw new InvalidOperationException(Constants.ErrorMessages.NotAllowed);
                }

                var invitation = context.Invitations.Where(i => i.TeamId == team.Id && i.InvitedUserId == user.Id && i.IsActive == true);

                if (invitation.Any())
                {
                    throw new InvalidOperationException(Constants.ErrorMessages.InviteIsAlreadySent);
                }
            }

            invitationService.InviteToTeam(teamName, username);

            return($"Team {teamName} invited {username}!");
        }
示例#8
0
        public static bool IsTeamExisting(string teamName)
        {
            using (var context = new TeamBuilderDbContext())
            {
                var result = context.Teams.Any(t => t.Name == teamName);

                return(result);
            }
        }
示例#9
0
        public static bool IsUserCreatorOfTeam(string teamName, User creator)
        {
            using (var context = new TeamBuilderDbContext())
            {
                var result = context.Teams.Any(t => t.Name == teamName && t.Creator == creator);

                return(result);
            }
        }
示例#10
0
        public static bool IsUserExisting(string username)
        {
            using (var context = new TeamBuilderDbContext())
            {
                var result = context.Users.Any(u => u.Username == username);

                return(result);
            }
        }
示例#11
0
        public User ById(int id)
        {
            using (var context = new TeamBuilderDbContext())
            {
                var user = context.Users.Find(id);

                return(user);
            }
        }
示例#12
0
        public User ByUsername(string username)
        {
            using (var context = new TeamBuilderDbContext())
            {
                var user = context.Users.Single(u => u.Username == username);

                return(user);
            }
        }
 public static bool IsMemberOfTeam(string teamName, string username)
 {
     using (TeamBuilderDbContext context = new TeamBuilderDbContext())
     {
         return(context.Teams
                .Single(t => t.Name == teamName)
                .UserTeams.Any(ut => ut.User.Username == username));
     }
 }
 public static bool IsUserCreatorOfTeam(string teamName, User user)
 {
     using (TeamBuilderDbContext context = new TeamBuilderDbContext())
     {
         return(context
                .Teams
                .Any(t => t.Name == teamName && t.Creator == user));
     }
 }
 public static bool IsUserCreatorOfEvent(string eventName, User user)
 {
     using (TeamBuilderDbContext context = new TeamBuilderDbContext())
     {
         return(context
                .Events
                .Any(e => e.Name == eventName && e.Creator == user));
     }
 }
示例#16
0
        public static bool IsUserCreatorOfEvent(string eventName, User creator)
        {
            using (var context = new TeamBuilderDbContext())
            {
                var result = context.Events
                             .Any(e => e.Name == eventName && e.Creator == creator);

                return(result);
            }
        }
示例#17
0
        public static bool IsEventExisting(string eventName)
        {
            using (var context = new TeamBuilderDbContext())
            {
                var result = context.Events
                             .Any(e => e.Name == eventName);

                return(result);
            }
        }
示例#18
0
        public static bool IsInviteExisting(string teamName, User user)
        {
            using (var context = new TeamBuilderDbContext())
            {
                var result = context.Invitations
                             .Any(i => i.Team.Name == teamName && i.InvitedUser == user);

                return(result);
            }
        }
示例#19
0
        public void Delete(int id)
        {
            using (var context = new TeamBuilderDbContext())
            {
                var user = context.Users.Find(id);

                context.Users.Remove(user);
                context.SaveChanges();
            }
        }
示例#20
0
        public User ByUsernameAndPassword(string username, string password)
        {
            using (var context = new TeamBuilderDbContext())
            {
                var user = context.Users
                           .Single(u => u.Username == username &&
                                   u.Password == password);

                return(user);
            }
        }
示例#21
0
        public static bool IsMemberOfTeam(string teamName, string username)
        {
            using (var context = new TeamBuilderDbContext())
            {
                var result = context.Teams
                             .Any(t => t.Name == teamName && t.Members
                                  .Any(ut => ut.User.Username == username));

                return(result);
            }
        }
示例#22
0
        public string Execute(string[] args)
        {
            Checker.CheckArgumentsLength(EXPRECTED_ARGUMENTS_LENGTH, args.Length);
            Checker.CheckUserIsLoggedOut();

            var loggedInUser   = AuthenticationService.GetCurrentUser();
            var teamName       = args[0];
            var usernameToKick = args[1];

            if (!DatabaseChecker.IsTeamExisting(teamName))
            {
                throw new ArgumentException(string.Format(ErrorMessages.TEAM_NOT_FOUND, teamName));
            }

            if (!DatabaseChecker.IsUserExisting(usernameToKick))
            {
                throw new ArgumentException(string.Format(ErrorMessages.USER_NOT_FOUND, usernameToKick));
            }

            if (!DatabaseChecker.IsMemberOfTeam(teamName, loggedInUser.Username))
            {
                throw new ArgumentException(string.Format(ErrorMessages.NOT_PART_OF_TEAM, loggedInUser.Username, teamName));
            }

            if (!DatabaseChecker.IsMemberOfTeam(teamName, usernameToKick))
            {
                throw new ArgumentException(string.Format(ErrorMessages.NOT_PART_OF_TEAM, usernameToKick, teamName));
            }

            if (!DatabaseChecker.IsUserCreatorOfTeam(teamName, loggedInUser))
            {
                throw new InvalidOperationException(ErrorMessages.OPERATION_NOT_ALLOWED);
            }

            User userToKick = null;

            using (var context = new TeamBuilderDbContext())
            {
                userToKick = context.Users.FirstOrDefault(u => u.Username == usernameToKick);
            }

            if (DatabaseChecker.IsUserCreatorOfTeam(teamName, userToKick))
            {
                throw new InvalidOperationException(string.Format(ErrorMessages.COMMAND_NOT_ALLOWED, "DisbandTeam"));
            }

            this.userService.KickMember(teamName, usernameToKick);

            var message = string.Format(SuccessfullMessages.SUCCESSFULLY_KICKED_USER, usernameToKick, teamName);

            return(message);
        }
示例#23
0
        public string Execute(params string[] args)
        {
            Check.CheckLength(2, args);

            var teamName = args[0];
            var username = args[1];

            using (var context = new TeamBuilderDbContext())
            {
                var user = context.Users.SingleOrDefault(u => u.Username == username);

                var isTeamExist = CommandHelper.IsTeamExisting(teamName);
                if (!isTeamExist)
                {
                    throw new ArgumentException(string.Format(Constants.ErrorMessages.TeamNotFound, teamName));
                }

                var isUserExist = CommandHelper.IsUserExisting(username);
                if (!isUserExist)
                {
                    throw new ArgumentException(string.Format(Constants.ErrorMessages.UserNotFound, username));
                }

                var isMemberOfTeam = CommandHelper.IsMemberOfTeam(teamName, username);
                if (!isMemberOfTeam)
                {
                    throw new ArgumentException(string.Format(Constants.ErrorMessages.NotPartOfTeam, username, teamName));
                }

                if (Session.User == null)
                {
                    throw new InvalidOperationException(Constants.ErrorMessages.LoginFirst);
                }

                var isCreatorOfTeam = CommandHelper.IsUserCreatorOfTeam(teamName, Session.User);
                if (!isCreatorOfTeam)
                {
                    throw new InvalidOperationException(Constants.ErrorMessages.NotAllowed);
                }

                var isUserToKickIsCreator = CommandHelper.IsUserCreatorOfTeam(teamName, user);
                if (isUserToKickIsCreator)
                {
                    throw new InvalidOperationException($"Command not allowed. Use DisbandTeam instead.");
                }

                teamService.KickMember(teamName, username);
            }

            return($"User {username} was kicked from {teamName}!");
        }
示例#24
0
        public static bool isTeamAlreadyAdded(string teamName, string eventName)
        {
            using (var context = new TeamBuilderDbContext())
            {
                var @event = context.Events
                             .Where(e => e.Name == eventName)
                             .OrderByDescending(e => e.StartDate)
                             .First();

                var result = context.EventTeams
                             .Any(et => et.EventId == @event.Id && et.Team.Name == teamName);

                return(result);
            }
        }
        public string Execute(string[] args)
        {
            Checker.CheckArgumentsLength(EXPRECTED_ARGUMENTS_LENGTH, args.Length);
            Checker.CheckUserIsLoggedOut();

            var teamName = args[0];
            var username = args[1];

            User inviteReciever = null;
            var  loggedInUser   = AuthenticationService.GetCurrentUser();

            using (var context = new TeamBuilderDbContext())
            {
                inviteReciever = context.Users.FirstOrDefault(u => u.Username == username);
            }

            if (!DatabaseChecker.IsUserExisting(username) || !DatabaseChecker.IsTeamExisting(teamName))
            {
                throw new ArgumentException(ErrorMessages.TEAM_OR_USER_NOT_EXIST);
            }

            var isUserCreator              = DatabaseChecker.IsUserCreatorOfTeam(teamName, loggedInUser);
            var isCreatorPartOfTheTeam     = DatabaseChecker.IsMemberOfTeam(teamName, loggedInUser.Username);
            var isInvitedUserPartOfTheTeam = DatabaseChecker.IsMemberOfTeam(teamName, username);
            var isUserInvited              = DatabaseChecker.IsMemberOfTeam(teamName, username);
            var isUserDeleted              = DatabaseChecker.IsUserDeleted(username);

            if (!isUserCreator || !isCreatorPartOfTheTeam || isUserInvited || isUserDeleted || isInvitedUserPartOfTheTeam)
            {
                throw new InvalidOperationException(ErrorMessages.OPERATION_NOT_ALLOWED);
            }

            if (DatabaseChecker.IsInviteExisting(teamName, inviteReciever))
            {
                throw new InvalidOperationException(ErrorMessages.INVITE_IS_ALREADY_SENT);
            }

            this.userService.SendInvitation(teamName, inviteReciever);

            var message = string.Format(SuccessfullMessages.SUCCESSFULLY_MADE_INVITATION, teamName, username);

            return(message);
        }
示例#26
0
        public void Run()
        {
            using (var context = new TeamBuilderDbContext())
            {
                context.Database.EnsureDeleted();
                context.Database.EnsureCreated();
            }

            while (true)
            {
                try
                {
                    string input = Console.ReadLine();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.GetBaseException().Message);
                }
            }
        }
示例#27
0
        public User Create(string username, string password, string firstName, string lastName, int age, Gender gender)
        {
            using (var context = new TeamBuilderDbContext())
            {
                var user = new User()
                {
                    Username  = username,
                    Password  = password,
                    FirstName = firstName,
                    LastName  = lastName,
                    Age       = age,
                    Gender    = gender
                };

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

                return(user);
            }
        }
示例#28
0
        public string Execute(params string[] args)
        {
            Check.CheckLength(1, args);

            var eventName = args[0];

            using (var context = new TeamBuilderDbContext())
            {
                var isEventExist = CommandHelper.IsEventExisting(eventName);
                if (!isEventExist)
                {
                    throw new ArgumentException(string.Format(Constants.ErrorMessages.EventNotFound, eventName));
                }

                var @event = context.Events
                             .Where(e => e.Name == eventName)
                             .OrderByDescending(e => e.StartDate)
                             .First();

                var builder = new StringBuilder();

                builder.AppendLine($"[{eventName}] [{@event.StartDate}] [{@event.EndDate}] [{@event.Description}]");
                builder.AppendLine("Teams:");

                var eventTeams = context.EventTeams
                                 .Where(et => et.EventId == @event.Id)
                                 .ToList();

                foreach (var eventTeam in eventTeams)
                {
                    var team = context.Teams.Find(eventTeam.TeamId);
                    builder.AppendLine($"-{team.Name}");
                }

                var result = builder.ToString().Trim();

                return(result);
            }
        }
示例#29
0
        public string Execute(params string[] args)
        {
            Check.CheckLength(1, args);

            var teamName = args[0];

            using (var context = new TeamBuilderDbContext())
            {
                var isTeamExist = CommandHelper.IsTeamExisting(teamName);
                if (!isTeamExist)
                {
                    throw new ArgumentException(string.Format(Constants.ErrorMessages.TeamNotFound, teamName));
                }

                var team = context.Teams.FirstOrDefault(t => t.Name == teamName);

                var builder = new StringBuilder();

                builder.AppendLine($"[{team.Name}] [{team.Acronym}]");
                builder.AppendLine($"Members:");

                var teamMembers = context.UserTeams
                                  .Where(ut => ut.TeamId == team.Id)
                                  .ToArray();

                foreach (var teamMember in teamMembers)
                {
                    var member = context.Users.Find(teamMember.UserId);

                    builder.AppendLine($"--{member.Username}");
                }

                var result = builder.ToString().Trim();

                return(result);
            }
        }
示例#30
0
 public EventService(TeamBuilderDbContext context)
 {
     this.context = context;
 }