//<teamName> public static string Execute(string[] data) { Check.CheckLenght(1, data); //login first AuthenticationManager.Autorized(); var currentUser = AuthenticationManager.GetCurrentUser(); string teamName = data[0]; bool isTeamExist = CommandHelper.IsTeamExisting(teamName); if (!isTeamExist) { string errorMsg = string.Format(Constants.ErrorMessages.TeamNotFound, teamName); throw new ArgumentException(errorMsg); } bool isInviteExist = CommandHelper.IsInviteExisting(teamName, currentUser); if (!isInviteExist) { string errorMsg = string.Format(Constants.ErrorMessages.InviteNotFound, teamName); throw new ArgumentException(errorMsg); } using (var db = new TeamBuilderContext()) { var currentInvitation = db.Invitations .FirstOrDefault(i => i.Team.Name == teamName && i.InvitedUserId == currentUser.UserId && i.IsActive); currentInvitation.IsActive = false; db.SaveChanges(); } return($"Invite from {teamName} declined."); }
//<teamName> public static string Execute(string[] data) { Check.CheckLenght(1, data); AuthenticationManager.Autorized(); var currentUser = AuthenticationManager.GetCurrentUser(); string teamName = data[0]; if (!CommandHelper.IsTeamExisting(teamName)) { string errorMsg = string.Format(Constants.ErrorMessages.TeamNotFound, teamName); throw new ArgumentException(errorMsg); } using (var db = new TeamBuilderContext()) { var team = db.Teams //.Include(t => t.Creator) .SingleOrDefault(t => t.Name == teamName); if (currentUser.UserId != team.CreatorId) { throw new InvalidOperationException(Constants.ErrorMessages.NotAllowed); } var teamAndMembers = db.UsersTeams.Where(t => t.TeamId == team.TeamId); db.UsersTeams.RemoveRange(teamAndMembers); db.Teams.Remove(team); db.SaveChanges(); } return($"{teamName} has disbanded!"); }
//<eventName> <teamName> public static string Execute(string[] data) { Check.CheckLenght(2, data); AuthenticationManager.Autorized(); var currentUser = AuthenticationManager.GetCurrentUser(); string eventName = data[0]; string teamName = data[1]; if (!CommandHelper.IsEventExisting(eventName)) { string errorMsg = string.Format(Constants.ErrorMessages.EventNotFound, eventName); throw new ArgumentException(errorMsg); } if (!CommandHelper.IsTeamExisting(teamName)) { string errorMsg = string.Format(Constants.ErrorMessages.TeamNotFound, teamName); throw new ArgumentException(errorMsg); } if (!CommandHelper.IsUserCreatorOfEvent(eventName, currentUser)) { throw new InvalidOperationException(Constants.ErrorMessages.NotAllowed); } var currentEvent = CommandHelper.GetLatestEvent(eventName); using (var db = new TeamBuilderContext()) { var team = db.Teams.SingleOrDefault(t => t.Name == teamName); bool isTeamAddedToEvent = db.TeamsEvents.Any(te => te.EventId == currentEvent.EventId && te.TeamId == team.TeamId); if (isTeamAddedToEvent) { throw new InvalidOperationException(Constants.ErrorMessages.CannotAddSameTeamTwice); } var teamToAddToEvent = new TeamEvent { TeamId = team.TeamId, EventId = currentEvent.EventId }; db.TeamsEvents.Add(teamToAddToEvent); db.SaveChanges(); } return($"Team {teamName} added for {eventName}!"); }
//<name> <acronym> <description> public static string Execute(string[] data) { Check.CheckCreateTeamCommandLength(2, 3, data); //login first AuthenticationManager.Autorized(); var currentUser = AuthenticationManager.GetCurrentUser(); string teamName = data[0]; if (CommandHelper.IsTeamExisting(teamName)) { string errorMsg = string.Format(Constants.ErrorMessages.TeamExists, teamName); throw new ArgumentException(errorMsg); } string acronym = data[1]; if (acronym.Length != 3) { string errorMsg = string.Format(Constants.ErrorMessages.InvalidAcronym, acronym); throw new ArgumentException(errorMsg); } string description = data.Length == 3 ? data[2] : null; var team = new Team { Name = teamName, Description = description, Acronym = acronym, CreatorId = currentUser.UserId }; var usersInTeam = new UserTeam { Team = team, UserId = currentUser.UserId }; using (var db = new TeamBuilderContext()) { db.Teams.Add(team); db.UsersTeams.Add(usersInTeam); db.SaveChanges(); } return($"Team {teamName} successfully created!"); }
// <name> <description> <startDate> <endDate> public static string Execute(string[] data) { Check.CheckLenght(6, data); //login first AuthenticationManager.Autorized(); string[] inputStartDateArr = data.Skip(2).Take(2).ToArray(); string inputStartDate = string.Join(" ", inputStartDateArr); string[] inputEndDateArr = data.Skip(4).Take(2).ToArray(); string inputEndDate = string.Join(" ", inputEndDateArr); DateTime startDate; DateTime endDate; var isValidStartDate = DateTime.TryParseExact(inputStartDate, Constants.DateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out startDate); var isValidEndDate = DateTime.TryParseExact(inputEndDate, Constants.DateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out endDate); if (!isValidStartDate || !isValidEndDate) { throw new ArgumentException(Constants.ErrorMessages.InvalidDateFormat); } if (startDate > endDate) { throw new ArgumentException("Start date should be before end date."); } string name = data[0]; string description = data[1]; var currentUser = AuthenticationManager.GetCurrentUser(); var newEvent = new Event { Name = name, Description = description, StartDate = startDate, EndDate = endDate, CreatorId = currentUser.UserId }; using (var db = new TeamBuilderContext()) { db.Events.Add(newEvent); db.SaveChanges(); } return($"Event {name} was created successfully!"); }
//[no args] public static string Execute(string[] data) { Check.CheckLenght(0, data); //if there is no logged user = exception AuthenticationManager.Autorized(); var currentUser = AuthenticationManager.GetCurrentUser(); using (var db = new TeamBuilderContext()) { var user = db.Users.SingleOrDefault(c => c.Username == currentUser.Username); user.IsDeleted = true; db.SaveChanges(); AuthenticationManager.Logout(); } return($"User {currentUser.Username} was deleted successfully!"); }
//<teamName> <username> public static string Execute(string[] data) { Check.CheckLenght(2, data); //loginFirst AuthenticationManager.Autorized(); var currentUser = AuthenticationManager.GetCurrentUser(); string teamName = data[0]; if (!CommandHelper.IsTeamExisting(teamName)) { string errorMsg = string.Format(Constants.ErrorMessages.TeamNotFound, teamName); throw new ArgumentException(errorMsg); } string username = data[1]; if (!CommandHelper.IsUserExisting(username)) { string errorMsg = string.Format(Constants.ErrorMessages.UserNotFound, username); throw new ArgumentException(errorMsg); } bool isUsernameMemberOfTeam = CommandHelper.IsMemberOfTeam(teamName, username); if (!isUsernameMemberOfTeam) { string errorMsg = string.Format(Constants.ErrorMessages.NotPartOfTeam, username, teamName); throw new ArgumentException(errorMsg); } if (!CommandHelper.IsUserCreatorOfTeam(teamName, currentUser)) { throw new InvalidOperationException(Constants.ErrorMessages.NotAllowed); } using (var db = new TeamBuilderContext()) { var team = db.Teams .Include(t => t.Creator) .Include(ut => ut.UsersTeam) .SingleOrDefault(t => t.Name == teamName); if (team.Creator.Username == username) { string errorMsg = string.Format(Constants.ErrorMessages.CommandNotAllowed, "DisbandTeam"); throw new InvalidOperationException(errorMsg); } var user = db.Users.SingleOrDefault(u => u.Username == username); var userTeamToRemove = db.UsersTeams .SingleOrDefault(u => u.UserId == user.UserId && u.TeamId == team.TeamId); db.UsersTeams.Remove(userTeamToRemove); db.SaveChanges(); return($"User {username} was kicked from {teamName}!"); } }
//<teamName> <username> public static string Execute(string[] data) { Check.CheckLenght(2, data); //login first AuthenticationManager.Autorized(); var currentUser = AuthenticationManager.GetCurrentUser(); string teamName = data[0]; string username = data[1]; if (!CommandHelper.IsUserExisting(username) || !CommandHelper.IsTeamExisting(teamName)) { throw new ArgumentException(Constants.ErrorMessages.TeamOrUserNotExist); } using (var db = new TeamBuilderContext()) { var team = db.Teams.SingleOrDefault(t => t.Name == teamName); var invitedUser = db.Users.SingleOrDefault(u => u.Username == username); if (CommandHelper.IsInviteExisting(teamName, invitedUser)) { throw new InvalidOperationException(Constants.ErrorMessages.InviteIsAlreadySent); } bool isUserCreatorOfTeam = CommandHelper.IsUserCreatorOfTeam(teamName, currentUser); bool isCurrentUserMemberOfTheTeam = CommandHelper.IsMemberOfTeam(teamName, currentUser.Username); bool isInvitedUserMemberOfTeam = CommandHelper.IsMemberOfTeam(teamName, invitedUser.Username); if ((!isUserCreatorOfTeam && !isCurrentUserMemberOfTheTeam) && isInvitedUserMemberOfTeam) { throw new InvalidOperationException(Constants.ErrorMessages.NotAllowed); } if (isUserCreatorOfTeam) { var invitation = new Invitation { InvitedUserId = invitedUser.UserId, TeamId = team.TeamId, IsActive = false }; var addInvitedUserToTeam = new UserTeam { TeamId = team.TeamId, UserId = invitedUser.UserId }; db.Invitations.Add(invitation); db.UsersTeams.Add(addInvitedUserToTeam); db.SaveChanges(); } else { var invitation = new Invitation { InvitedUserId = invitedUser.UserId, TeamId = team.TeamId }; db.Invitations.Add(invitation); db.SaveChanges(); } } return($"Team {teamName} invited {username}!"); }