public string Execute(string[] args) { Check.CheckLength(1, args); AuthenticationManager.Authorize(); string teamName = args[0]; User currentUser = AuthenticationManager.GetCurrentUser(); if (!CommandHelper.IsUserCreatorOfTeam(teamName, currentUser)) { throw new InvalidOperationException(Constants.ErrorMessages.NotAllowed); } if (!CommandHelper.IsTeamExisting(teamName)) { throw new ArgumentException(string.Format(Constants.ErrorMessages.TeamNotFound, teamName)); } using (var context = new TeamBuilderContext()) { Team team = context.Teams.FirstOrDefault(t => t.Name == teamName); UserTeam userTeams = currentUser.UserTeams.FirstOrDefault(ut => ut.Team.Name == teamName); currentUser.CreatedTeams.Remove(team); currentUser.UserTeams.Remove(userTeams); context.Teams.Remove(team); context.Entry(currentUser).State = EntityState.Modified; context.SaveChanges(); } return($"{teamName} has disbanded!"); }
public string Execute(string[] args) { Check.CheckLength(2, args); AuthenticationManager.Authorize(); string eventName = args[0]; string teamName = args[1]; if (!CommandHelper.IsEventExisting(eventName)) { throw new ArgumentException(string.Format(Constants.ErrorMessages.EventNotFound, eventName)); } else 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); } else if (CommandHelper.TeamIsMemberOfEvent(eventName, teamName)) { throw new InvalidOperationException(Constants.ErrorMessages.CannotAddSameTeamTwice); } using (var context = new TeamBuilderContext()) { Team team = context.Teams .Include(t => t.UserTeams) .FirstOrDefault(t => t.Name == teamName); Event ev = context.Events .Include(e => e.ParticipatingEventTeams) .FirstOrDefault(e => e.Name == eventName); var eventTeam = new EventTeam { Team = team, TeamId = team.Id, Event = ev, EventId = ev.Id }; team.Events.Add(eventTeam); ev.ParticipatingEventTeams.Add(eventTeam); context.Entry(eventTeam).State = EntityState.Added; context.SaveChanges(); } return($"Team {teamName} added for {eventName}!"); }
public override string Execute(TeamBuilderContext context) { base.MustBeLoggedIn(); context.Entry(this.Session.User).State = EntityState.Unchanged; this.Session.User.IsDeleted = true; context.SaveChanges(); var username = this.Session.User.Username; this.Session.LogOut(); return(string.Format(Success, username)); }
public string Execute(string[] args) { Check.CheckLength(1, args); string teamName = args[0]; User currentUser = AuthenticationManager.GetCurrentUser(); CommandHelper.AllowInviteActionsOrThrow(teamName, currentUser); Invitation invitation = currentUser.ReceivedInvitaions .FirstOrDefault(i => i.Team.Name == teamName); using (var context = new TeamBuilderContext()) { invitation.IsActive = false; Team team = invitation.Team; UserTeam userTeam = new UserTeam { User = currentUser, UserId = currentUser.Id, Team = team, TeamId = team.Id }; context.Entry(invitation).State = EntityState.Modified; context.Entry(currentUser).State = EntityState.Modified; context.Entry(team).State = EntityState.Modified; context.Entry(userTeam).State = EntityState.Added; team.UserTeams.Add(userTeam); currentUser.UserTeams.Add(userTeam); context.SaveChanges(); } return($"User {currentUser.Username} joined team {teamName}!"); }
public string Execute(string[] args) { Check.CheckLength(0, args); AuthenticationManager.Authorize(); User currentUser = AuthenticationManager.GetCurrentUser(); using (var context = new TeamBuilderContext()) { context.Entry(currentUser).State = EntityState.Modified; currentUser.IsDeleted = true; context.SaveChanges(); AuthenticationManager.Logout(); } return($"User {currentUser.Username} was deleted successfully!"); }
private void _CreateTeam(string teamName, string teamAcronym, string description) { User currentUser = AuthenticationManager.GetCurrentUser(); using (var context = new TeamBuilderContext()) { context.Entry(currentUser).State = EntityState.Modified; var team = new Team { Name = teamName, Acronym = teamAcronym, Description = description, Creator = currentUser, CreatorId = currentUser.Id }; context.Teams.Add(team); context.SaveChanges(); } }
public string Execute(string[] args) { Check.CheckLength(4, args); string eventName = args[0]; string description = args[1]; DateTime startDate; DateTime endDate; if (!DateTime.TryParseExact(args[2], "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out startDate) || !DateTime.TryParseExact(args[3], "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out endDate)) { throw new ArgumentException(Constants.ErrorMessages.InvalidDateFormat); } else if (startDate > endDate) { throw new ArgumentException("Start date should be before end date."); } AuthenticationManager.Authorize(); using (var context = new TeamBuilderContext()) { var currentUser = AuthenticationManager.GetCurrentUser(); context.Entry(currentUser).State = EntityState.Modified; context.Events.Add(new Event { Name = eventName, Description = description, StartDate = startDate, EndDate = endDate, Creator = currentUser, CreatorId = currentUser.Id }); context.SaveChanges(); } return($"Event {eventName} was created successfully!"); }
public string Execute(string[] inputArgs) { Check.CheckLength(6, inputArgs); string name = inputArgs[0]; if (name.Length > 25 || string.IsNullOrWhiteSpace(name)) { throw new ArgumentException("Invalid event name!"); } string description = inputArgs[1]; if (description.Length > 250 || string.IsNullOrWhiteSpace(description)) { throw new ArgumentException("Invalid event description!"); } AuthenticationManager.Authorize(); var user = AuthenticationManager.GetCurrentUser(); StringBuilder start = new StringBuilder(); start.Append($"{inputArgs[2]} {inputArgs[3]}"); string tryParseStartDate = start.ToString().Trim(); StringBuilder end = new StringBuilder(); end.Append($"{inputArgs[4]} {inputArgs[5]}"); string tryParseEndDate = end.ToString().Trim(); DateTime startDate; bool isValidStartDate = DateTime.TryParseExact(tryParseStartDate, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out startDate); DateTime endDate; bool isValidEndDate = DateTime.TryParseExact(tryParseEndDate, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out endDate); if (!isValidStartDate || !isValidEndDate) { throw new ArgumentException("Please insert the dates in format: [dd/MM/yyyy HH:mm]!"); } if (startDate > endDate) { throw new ArgumentException("Start date should be before end date."); } using (var db = new TeamBuilderContext()) { db.Entry(user).State = EntityState.Unchanged; Event currEvent = new Event() { Name = name, Description = description, StartDate = startDate, EndDate = endDate, CreatorId = user.UserId }; db.Events.Add(currEvent); db.SaveChanges(); } return($"Event {name} created successfully!"); }