public static SettingDTO GetSettings(int userId) { ChatLocoContext DbContext = new ChatLocoContext(); SettingDTO settings = DbContext.Settings.Where(s => s.UserId == userId).FirstOrDefault(); return(settings); }
public static bool DoesUserExist(int id) { ChatLocoContext DbContext = new ChatLocoContext(); UserDTO user = DbContext.Users.FirstOrDefault(u => u.Id == id); return(user != null); }
public static List <ErrorModel> makeUserAdmin(string uName) { List <ErrorModel> errors = new List <ErrorModel>(); ChatLocoContext db = new ChatLocoContext(); UserDTO user = db.Users.FirstOrDefault(u => u.Username == uName); //If user does not exist or they are an admin, fail. Otherwise make the user an admin. if (user == null) { errors.Add(new ErrorModel("User " + uName + " does not exist.")); return(errors); } if (user.Role == RoleLevel.Admin) { errors.Add(new ErrorModel("User " + uName + " is already an administrator.")); return(errors); } if (user.Role == RoleLevel.Blocked) { errors.Add(new ErrorModel("User " + uName + " is currently blocked.")); } user.Role = RoleLevel.Admin; db.SaveChanges(); return(errors); }
public static SettingDTO CreateSettings(int userId, string defaulthandle, string email) { ChatLocoContext DbContext = new ChatLocoContext(); try { SettingDTO setting = new SettingDTO() { UserId = userId, DefaultHandle = defaulthandle, Email = email }; //add it to the table and commit the changes DbContext.Settings.Add(setting); DbContext.SaveChanges(); SettingsCache.Add(setting.Id, setting); return(setting); } catch (Exception e) { return(null); } }
public static List <ErrorModel> RemoveUser(RemoveUserRequestModel request) { List <ErrorModel> errors = new List <ErrorModel>(); if (!DoesUserExist(request.Username)) { errors.Add(new ErrorModel("user.no.exist")); return(errors); } ChatLocoContext db = new ChatLocoContext(); UserDTO user = db.Users.FirstOrDefault(u => u.Username == request.Username); if (user == null) { errors.Add(new ErrorModel("system.database.error")); return(errors); } SettingDTO settings = db.Settings.FirstOrDefault(s => s.UserId == user.Id); if (settings != null) { db.Settings.Remove(settings); } db.Users.Remove(user); db.SaveChanges(); return(errors); }
public static bool DoesUserExist(string username) { ChatLocoContext db = new ChatLocoContext(); UserDTO user = db.Users.FirstOrDefault(u => u.Username == username); return(user != null); }
public static List <MessageDTO> GetRecentChatroomMessages(int chatroomId, int amount) { var messages = new List <MessageDTO>(); ChatLocoContext DbContext = new ChatLocoContext(); var results = DbContext.Messages.Where(m => m.ChatroomId == chatroomId && m.DateCreated != null).OrderBy(m => m.DateCreated).Take(amount); messages.AddRange(results); return(messages); }
public static List <ErrorModel> MakeActivated(int uid) { var errors = new List <ErrorModel>(); ChatLocoContext db = new ChatLocoContext(); UserDTO user = db.Users.FirstOrDefault(u => u.Id == uid); user.IsActivated = true; db.SaveChanges(); return(errors); }
public static LoginResponseModel GetLoginResponseModel(LoginRequestModel request) { var response = new LoginResponseModel(); string passwordHash = SecurityService.GetStringSha256Hash(request.Password); var user = GetUser(request.Username); if (user == null) { response.LoginErrors.Add(new ErrorModel("Username not found.")); return(response); } if (user.Role == RoleLevel.Blocked) { response.LoginErrors.Add(new ErrorModel("This account has been blocked.<br />Please reach out to us via our contact page if you want to be unblocked.")); return(response); } if (!passwordHash.Equals(user.PasswordHash, StringComparison.Ordinal)) { response.LoginErrors.Add(new ErrorModel("Incorrect password.")); return(response); } var db = new ChatLocoContext(); user.LastLoginDate = DateTime.Now; db.SaveChanges(); //var userModel = Mapper.Map<UserDTO, UserModel>(user); var userModel = new UserModel() { Id = user.Id, Username = user.Username, Role = user.Role, IsActivated = user.IsActivated }; var userSettings = SettingService.GetSettings(userModel.Id); userModel.Settings = new UserSettingsModel() { DefaultHandle = userSettings.DefaultHandle, Email = userSettings.Email }; response.User = userModel; return(response); }
public static UserInfoResponseModel GetUserInfoResponseModel(UserInfoRequestModel request) { var response = new UserInfoResponseModel(); var userSettings = SettingService.GetSettings(request.Id); ChatLocoContext DbContext = new ChatLocoContext(); UserDTO user = DbContext.Users.FirstOrDefault(u => u.Id == request.Id); response.DefaultHandle = userSettings.DefaultHandle; response.Username = user.Username; response.Email = userSettings.Email; return(response); }
public static List <ErrorModel> CreateUser(string username, string email, string password) { List <ErrorModel> errors = new List <ErrorModel>(); if (DoesUserExist(username)) { errors.Add(new ErrorModel("Username already exists.")); return(errors); } ChatLocoContext db = new ChatLocoContext(); if (db.Settings.FirstOrDefault(s => s.Email == email) != null) { errors.Add(new ErrorModel("Email already in use.")); return(errors); } string passwordHash = SecurityService.GetStringSha256Hash(password); UserDTO user = new UserDTO() { JoinDate = DateTime.Now, LastLoginDate = null, PasswordHash = passwordHash, Username = username, Role = RoleLevel.User, IsActivated = true }; db.Users.Add(user); db.SaveChanges(); //var l = SendMailService.sendCreationCode(email, user.Id); SettingDTO settings = SettingService.CreateSettings(user.Id, user.Username, email);//the default handle is the users username if (settings == null) { //somehow failed to create user settings errors.Add(new ErrorModel("Failure to create User Setting!.")); return(errors); } return(errors); }
public static MessageDTO GetMessage(int messageId) { System.Diagnostics.Debug.WriteLine("getting meesages"); ChatLocoContext DbContext = new ChatLocoContext(); try { return(MessageCache[messageId]); //if it exists in the cache return it } catch (Exception e) //if it doesnt exist grab it from the database { MessageDTO message = DbContext.Messages.FirstOrDefault(msg => msg.Id == messageId); if (message != null) { MessageCache.Add(message.Id, message); } return(message); } }
public static List <ErrorModel> unblockUser(string uName) { List <ErrorModel> errors = new List <ErrorModel>(); ChatLocoContext db = new ChatLocoContext(); UserDTO user = db.Users.FirstOrDefault(u => u.Username == uName); if (user == null) { errors.Add(new ErrorModel("User " + uName + " does not exist.")); return(errors); } if (user.Role != RoleLevel.Blocked) { errors.Add(new ErrorModel("User is not currently blocked.")); return(errors); } user.Role = RoleLevel.User; db.SaveChanges(); return(errors); }
private void CreateAdminIfNotExist() { ChatLocoContext DbContext = new ChatLocoContext(); //if no admin exists then make the default one. if (DbContext.Users.FirstOrDefault(u => u.Role == RoleLevel.Admin) == null) { UserDTO adminUser = new UserDTO() { JoinDate = DateTime.Now, LastLoginDate = null, PasswordHash = SecurityService.GetStringSha256Hash("Admin"), Username = "******", Role = RoleLevel.Admin }; DbContext.Users.Add(adminUser); DbContext.SaveChanges(); SettingDTO settings = SettingService.CreateSettings(adminUser.Id, adminUser.Username, "*****@*****.**"); } }
public static UpdateSettingsResponseModel UpdateSettings(UpdateSettingsRequestModel request) { var response = new UpdateSettingsResponseModel(); var user = UserService.GetUser(request.Username); if (user == null) { response.SettingsErrors.Add(new ErrorModel("Username not found.")); return(response); } //database connection var db = new ChatLocoContext(); var updatedSettings = db.Settings.FirstOrDefault(x => x.UserId == user.Id); //get the settings entry from the database updatedSettings.DefaultHandle = request.Settings.DefaultHandle; //update the users default handle updatedSettings.Email = request.Settings.Email; //update the users email db.SaveChanges(); //Construct the UserSettingsResponseModel var userModel = new UserModel() { Id = user.Id, Username = user.Username }; userModel.Settings = new UserSettingsModel() { DefaultHandle = updatedSettings.DefaultHandle, Email = updatedSettings.Email }; response.User = userModel; return(response); }
//if chatroomId is -1, means a private chatroom public static List <MessageDTO> CreateMessages(ComposeMessageRequestModel request, bool isPrivate, string userHandle, int desiredUserId, string desiredUserHandle) { var userId = request.UserId; var chatroomId = isPrivate ? -1 : request.ChatroomId; var rawMessage = request.Message; string boldString = request.Bold ? "font-weight:bold;" : ""; string italicString = request.Italic ? "font-style: italic;" : ""; string colorString = desiredUserId == -1 ? "color:" + request.Color + ";" : "color:#0099FF;"; string styleString = string.Format("style=\"word-wrap:break-word; {0} {1} {2} \" ", colorString, boldString, italicString); ChatLocoContext DbContext = new ChatLocoContext(); try { List <MessageDTO> createdMessages = new List <MessageDTO>(); if (desiredUserId == -1) { MessageDTO m = new MessageDTO() { UserId = userId, ChatroomId = chatroomId, RawMessage = rawMessage, DateCreated = DateTime.Now, IntendedForUserId = desiredUserId, Style = styleString }; string currentTime = m.DateCreated.ToString("MM/dd [h:mm:ss tt]"); string formattedMessage = string.Format("{0} {1}: {2}", currentTime, userHandle, rawMessage); m.FormattedMessage = formattedMessage; DbContext.Messages.Add(m); DbContext.SaveChanges(); MessageCache.Add(m.Id, m); createdMessages.Add(m); } else { //Create the private whisper MessageDTO m = new MessageDTO() { UserId = userId, ChatroomId = chatroomId, RawMessage = rawMessage, DateCreated = DateTime.Now, IntendedForUserId = desiredUserId, Style = styleString }; string currentTime = m.DateCreated.ToString("MM/dd [h:mm:ss tt]"); string formattedMessage = string.Format("{0} {1} : {2}", currentTime, "From " + userHandle, rawMessage); m.FormattedMessage = formattedMessage; DbContext.Messages.Add(m); DbContext.SaveChanges(); MessageCache.Add(m.Id, m); createdMessages.Add(m); //Create the receipt MessageDTO m2 = new MessageDTO() { UserId = userId, ChatroomId = chatroomId, RawMessage = rawMessage, DateCreated = DateTime.Now, IntendedForUserId = userId, Style = styleString }; string formattedMessage2 = string.Format("{0} {1} : {2}", currentTime, "To " + desiredUserHandle, rawMessage); m2.FormattedMessage = formattedMessage2; DbContext.Messages.Add(m2); DbContext.SaveChanges(); MessageCache.Add(m2.Id, m2); createdMessages.Add(m2); } return(createdMessages); } catch (Exception e) { return(null); } }
public static UserDTO GetUser(int id) { ChatLocoContext DbContext = new ChatLocoContext(); return(DbContext.Users.FirstOrDefault(u => u.Id == id)); }
public static UserDTO GetUser(string username) { ChatLocoContext DbContext = new ChatLocoContext(); return(DbContext.Users.FirstOrDefault(u => u.Username == username)); }