public string Excecute(string[] args) { string teamName = args[0]; using (var db = new TeamBuilderContext()) { Team team = db.Teams .Include(t => t.Creator) .SingleOrDefault(t => t.Name == teamName); if (Session.User == null) { throw new InvalidOperationException("You should login first!"); } User currentUser = Session.User; if (team == null) { throw new ArgumentException($"Team {teamName} not found!"); } if (team.Creator.Username != currentUser.Username) { throw new InvalidOperationException("Not allowed!"); } db.Teams.Remove(team); db.SaveChanges(); return($"{teamName} has disbanded!"); } }
// <username> <password> <repeat-password> <firstName> <lastName> <age> <gender> public override string Execute(TeamBuilderContext context) { base.MustBeLoggedOut(); base.CmdArgsExactLengthValidation(ArgsExactLength); var username = this.CmdArgs[0]; var password = this.CmdArgs[1]; var repeatPassword = this.CmdArgs[2]; var firstName = this.CmdArgs[3]; var lastName = this.CmdArgs[4]; var age = int.Parse(this.CmdArgs[5]); Gender gender; var isGenderValid = Enum.TryParse(this.CmdArgs[6], out gender); if (!isGenderValid) { throw new ArgumentException(GenderNotValidExceptionMessage); } if (password != repeatPassword) { throw new InvalidOperationException(PasswordsDoNotMatchExceptionMessage); } var user = this.GetUser(context, username, password, firstName, lastName, age); context.Users.Add(user); context.SaveChanges(); this.Session.LogIn(user); return(string.Format(Success, username)); }
// <teamName> public override string Execute(TeamBuilderContext context) { base.MustBeLoggedIn(); base.CmdArgsExactLengthValidation(ArgsExactLength); var teamName = this.CmdArgs[0]; var team = this.GetTeam(context, teamName); if (team.TeamUsers.Any(tu => tu.UserId == this.Session.User.Id)) { throw new ArgumentException(string.Format(AlreadyMemberExceptionMessage, teamName)); } if (team.Invitations == null || team.Invitations.Count == 0) { throw new ArgumentException(string.Format(MissingInvitationExceptionMessage, teamName)); } var invitation = team.Invitations.First(); invitation.IsActive = false; context.SaveChanges(); return(string.Format(Success, teamName)); }
// <name> <description> <startDate> <endDate> public override string Execute(TeamBuilderContext context) { base.MustBeLoggedIn(); base.CmdArgsExactLengthValidation(ArgsExactLength); var creator = this.Session.User; var name = this.CmdArgs[0]; var description = this.CmdArgs[1]; var startDate = this.ParseDate(this.CmdArgs[2], this.CmdArgs[3]); var endDate = this.ParseDate(this.CmdArgs[4], this.CmdArgs[5]); if (startDate > endDate) { throw new ArgumentException(InvalidStartDateExceptionMessage); } /* ????????????????????????????? * There might be several events with the same name. * Always pick the one with the latest start date! */ var @event = new Event(); @event.Creator = creator; @event.Name = name; @event.Description = description; @event.StartDate = startDate; @event.EndDate = endDate; context.Events.Add(@event); context.SaveChanges(); return(string.Format(Success, name)); }
//<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."); }
public string Execute(string[] inputArgs) { Check.CheckLength(1, inputArgs); AuthenticationManager.Authorize(); string teamName = inputArgs[0]; if (!CommandHelper.IsTeamExisting(teamName)) { throw new ArgumentException(string.Format(Constants.ErrorMessages.TeamNotFound, teamName)); } User currentUser = AuthenticationManager.GetCurrentUser(); if (!CommandHelper.IsUserCreatorOfTeam(teamName, currentUser)) { throw new InvalidOperationException(Constants.ErrorMessages.NotAllowed); } using (var context = new TeamBuilderContext()) { Team team = context.Teams .Single(t => t.Name == teamName); context.Teams.Remove(team); context.SaveChanges(); } return($"{teamName} has disbanded!"); }
// • Disband<teamName> //Deletes given team.Allowed for the team’s creator only. public string Execute(string[] inputArgs) { Check.CheckLength(1, inputArgs); User user = AuthenticationManager.GetCurrentUser(); var teamName = inputArgs[0]; if (!CommandHelper.IsTeamExisting(teamName)) { throw new ArgumentException(string.Format(Constants.ErrorMessages.TeamNotFound, teamName)); } using (var context = new TeamBuilderContext()) { var team = context.Teams.FirstOrDefault(e => e.Name == teamName); if (user.Id != team.CreatorId) { throw new InvalidOperationException(Constants.ErrorMessages.NotAllowed); } context.Teams.Remove(team); context.SaveChanges(); } return($"{teamName} was disbanded!"); }
private static void DeleteTeam(string teamName) { using (var db = new TeamBuilderContext()) { var teamToBeDeleted = db .Teams .First(t => t.Name == teamName); var membershipsToBeDeleted = db .UserTeams .Where(ut => ut.TeamId == teamToBeDeleted.Id) .ToList(); var eventsToBeDeleted = db .TeamEvents .Where(te => te.TeamId == teamToBeDeleted.Id) .ToList(); var invitationsToBeDeleted = db .Invitations .Where(i => i.TeamId == teamToBeDeleted.Id) .ToList(); db.UserTeams.RemoveRange(membershipsToBeDeleted); db.TeamEvents.RemoveRange(eventsToBeDeleted); db.Invitations.RemoveRange(invitationsToBeDeleted); db.Teams.Remove(teamToBeDeleted); db.SaveChanges(); } }
public string Execute(string[] inputArgs) { string teamName = inputArgs[0]; AuthenticationManager.Authorize(); using (var db = new TeamBuilderContext()) { var team = db.Teams .FirstOrDefault(x => x.Name == teamName); var loggedUser = AuthenticationManager.GetCurrentUser(); if (team == null) { throw new ArgumentException($"Team {teamName} not found!"); } if (loggedUser.UserId != team.CreatorId) { throw new InvalidOperationException("Not allowed!"); } var invitations = db.Invitations .Where(x => x.TeamId == team.TeamId); db.Invitations.RemoveRange(invitations); db.Teams.Remove(team); db.SaveChanges(); } return($"{teamName} has disbanded!"); }
//<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!"); }
//Disband <teamName> internal static string Execute(string[] data) { Check.CheckLength(1, data); var currentUser = AuthenticationManager.GetCurrentUser(); var teamName = data[0]; bool isCurrentUserCreatorOfTeam = CommandHelper.IsUserCreatorOfTeam(teamName, currentUser); using (var context = new TeamBuilderContext()) { var team = context.Teams.FirstOrDefault(t => t.Name == teamName); if (!CommandHelper.IsTeamExisting(teamName)) { throw new ArgumentException(string.Format(Constants.ErrorMessages.TeamNotFound, teamName)); } if (!isCurrentUserCreatorOfTeam) { throw new InvalidOperationException(Constants.ErrorMessages.NotAllowed); } context.Teams.Remove(team); context.SaveChanges(); return($"{teamName} has disbanded!"); } }
public string Execute(string[] inputArgs) { string eventName = inputArgs[0]; string teamName = inputArgs[1]; if (String.IsNullOrWhiteSpace(teamName) || String.IsNullOrWhiteSpace(eventName)) { throw new InvalidOperationException("Invalid data."); } AuthenticationManager.Authorize(); using (var db = new TeamBuilderContext()) { var currEvent = db.Events .Include(x => x.Creator) .FirstOrDefault(x => x.Name == eventName); var team = db.Teams .FirstOrDefault(x => x.Name == teamName); var loggedUser = AuthenticationManager.GetCurrentUser(); if (currEvent == null) { throw new ArgumentException($"Event {eventName} not found!"); } if (team == null) { throw new ArgumentException($"Team {teamName} not found!"); } if (loggedUser.UserId != currEvent.CreatorId) { throw new InvalidOperationException("Not allowed!"); } bool alreadyAdded = db.EventTeams .Any(x => x.EventId == currEvent.EventId && x.TeamId == team.TeamId); if (alreadyAdded) { throw new ArgumentException($"Cannot add same team twice!"); } var eventTeam = new EventTeam() { EventId = currEvent.EventId, TeamId = team.TeamId }; currEvent.ParticipatingEventTeams.Add(eventTeam); db.SaveChanges(); } return($"Team {teamName} added for {eventName}!"); }
private void AddTeams(List <Team> teams) { using (var ctx = new TeamBuilderContext()) { ctx.Teams.AddRange(teams); ctx.SaveChanges(); } }
private void DeleteUser(User currentUser) { using (TeamBuilderContext context = new TeamBuilderContext()) { context.Users.SingleOrDefault(u => u.Id == currentUser.Id).IsDeleted = true; context.SaveChanges(); } }
private void AddUsers(ICollection <User> users) { using (TeamBuilderContext ctx = new TeamBuilderContext()) { ctx.Users.AddRange(users); ctx.SaveChanges(); } }
private void AddTeams(List <Team> teams) { using (TeamBuilderContext context = new TeamBuilderContext()) { context.Teams.AddRange(teams); context.SaveChanges(); } }
private static void DeleteUser(User user) { using (TeamBuilderContext ctx = new TeamBuilderContext()) { ctx.Users.SingleOrDefault(u => u.Id == user.Id).IsDeleted = true; ctx.SaveChanges(); } }
private void AddUsers(List <User> users) { using (TeamBuilderContext context = new TeamBuilderContext()) { context.Users.AddRange(users); context.SaveChanges(); } }
public static void DeleteUser(User user) { using (TeamBuilderContext context = new TeamBuilderContext()) { context.Users.Remove(user); context.SaveChanges(); } }
private void AddUsers(List <User> users) { using (var ctx = new TeamBuilderContext()) { ctx.Users.AddRange(users); ctx.SaveChanges(); } }
public static void AddUsers(List <User> users) { using (var db = new TeamBuilderContext()) { db.Users.AddOrUpdate(s => s.Username, users.ToArray()); db.SaveChanges(); } }
private void AddTeams(ICollection <Team> teams) { using (TeamBuilderContext ctx = new TeamBuilderContext()) { ctx.Teams.AddRange(teams); ctx.SaveChanges(); } }
public string Execute(string[] inputArgs) { string teamName = inputArgs[0]; string username = inputArgs[1]; if (String.IsNullOrWhiteSpace(teamName) || String.IsNullOrWhiteSpace(username)) { throw new InvalidOperationException("Invalid data."); } AuthenticationManager.Authorize(); using (var db = new TeamBuilderContext()) { var userToKick = db.Users .FirstOrDefault(x => x.Username == username); var team = db.Teams .Include(x => x.UserTeams) .ThenInclude(x => x.User) .FirstOrDefault(x => x.Name == teamName); var loggedUser = AuthenticationManager.GetCurrentUser(); if (userToKick == null) { throw new ArgumentException($"User {username} not found!"); } if (team == null) { throw new ArgumentException($"Team {teamName} not found!"); } if (loggedUser.UserId != team.CreatorId) { throw new InvalidOperationException("Not allowed!"); } if (team.CreatorId == userToKick.UserId) { throw new InvalidOperationException("Command not allowed. Use DisbandTeam instead."); } if (team.UserTeams.All(x => x.UserId != userToKick.UserId)) { throw new ArgumentException($"User {username} is not a member in {teamName}!"); } var userteam = db.UserTeams.FirstOrDefault(x => x.TeamId == team.TeamId && x.UserId == userToKick.UserId); team.UserTeams.Remove(userteam); db.SaveChanges(); } return($"User {username} was kicked from {teamName}!"); }
public string Execute(string[] inputArgs) { Check.CheckLength(2, inputArgs); AuthenticationManager.Authorize(); string eventName = inputArgs[0]; if (!CommandHelper.IsEventExisting(eventName)) { throw new ArgumentException(string.Format(Constants.ErrorMessages.EventNotFound, eventName)); } string teamName = inputArgs[1]; if (!CommandHelper.IsTeamExisting(teamName)) { throw new ArgumentException(string.Format(Constants.ErrorMessages.TeamNotFound, teamName)); } User currentUser = AuthenticationManager.GetCurrentUser(); if (!CommandHelper.IsUserCreatorOfEvent(eventName, currentUser)) { throw new InvalidOperationException(Constants.ErrorMessages.NotAllowed); } using (var context = new TeamBuilderContext()) { Event @event = context.Events .Where(e => e.Name == eventName) .OrderByDescending(et => et.StartDate) .First(); Team team = context.Teams .Single(t => t.Name == teamName); bool eventTeamExist = context.EventTeams .Any(et => et.EventId == @event.Id && et.TeamId == team.Id); if (eventTeamExist) { throw new InvalidOperationException(Constants.ErrorMessages.CannotAddSameTeamTwice); } var eventTeam = new EventTeam { Event = @event, Team = team }; context.EventTeams.Add(eventTeam); context.SaveChanges(); } return($"Team {teamName} added for {eventName}!"); }
private void DisbandTeam(string teamName) { using (TeamBuilderContext context = new TeamBuilderContext()) { Team team = context.Teams.FirstOrDefault(t => t.Name == teamName); context.Teams.Remove(team); context.SaveChanges(); } }
public static void AddEntity <TEntity>(object entity) where TEntity : class, new() { using (TeamBuilderContext context = new TeamBuilderContext()) { context.Add((TEntity)entity); context.SaveChanges(); } }
public static void AddTeams(List <Team> teams) { using (var db = new TeamBuilderContext()) { db.Teams.AddOrUpdate(t => t.Name, teams.ToArray()); db.SaveChanges(); } }
private static void UpdateInvitation(Invitation invitation) { using (TeamBuilderContext context = new TeamBuilderContext()) { invitation.IsActive = false; context.Invitations.Update(invitation); context.SaveChanges(); } }
public static void DisbandTeam(string teamName) { using (TeamBuilderContext db = new TeamBuilderContext()) { var team = db.Teams.FirstOrDefault(t => t.Name == teamName); db.Teams.Remove(team); db.SaveChanges(); } }
private void DisbandTeam(string teamName) { using (var ctx = new TeamBuilderContext()) { var team = ctx.Teams.FirstOrDefault(c => c.Name == teamName); ctx.Teams.Remove(team); ctx.SaveChanges(); } }