/// <summary> /// Lägger till en User i repositoriet /// </summary> /// <param name="newUser">Det User-objekt som skall läggas till</param> public void AddUser(User newUser) { if (newUser.UserID == null) throw new Exception("UserID is not allowed to be null!"); if (string.IsNullOrEmpty(newUser.UserName)) throw new Exception("UserName is not allowed to be empty!"); if (users.Any(u => u.UserID == newUser.UserID)) throw new Exception("A User with that UserID already Exists!"); users.Add(newUser); }
/* * OBS! Denna konstruktion för att ladda relaterad data är varken en bra eller vanlig konstruktion. * * Vi kommer se hur detta kan skötas i verkligheten senare i kursen * */ /// <summary> /// Ladda in en User-referens till ett Post-objekt /// </summary> /// <param name="users">En lista över alla giltiga User-objekt i applikationen</param> public void LoadUser(List<User> users) { _CreatedBy = users.Where(u => u.UserID == CreatedByID).FirstOrDefault(); }
//Create User State private string CreatUser(string input) { string result = ""; switch (input.ToLower()) { case "?": case "help": result = OutputHelper.AdminCommandList; break; case "exit": ParseState = State.Exit; result = OutputHelper.ExitMessage("Bye!"); break; case "save": if (UserName != "") { User newUser = new User(); newUser.UserName = UserName; repo.AddUser(newUser); OutputHelper.Put("New user was created!"); } else { OutputHelper.Put("Wrong username"); } break; case "cancel": OutputHelper.Put("you are now back in default state"); ParseState = State.Default; break; default: int InputNameStart = input.IndexOf(":"); if (InputNameStart > 0) { UserName = input.Substring(InputNameStart + 1).Trim(); OutputHelper.Put("If you want the username to be '" + UserName + "' write 'save' and the user will be created!"); } else { result = OutputHelper.ErrorInvalidInput; } break; } return result + OutputHelper.EnterCommand; }