public static Tuple <CommandModel, Guid> GetUserInfo(JObject param) { var token = param["Token"].ToString(); string authData = Encoding.UTF8.GetString(Convert.FromBase64String(token)); string[] tokenParts = authData.Split(':'); var email = tokenParts[0]; var password = tokenParts[1]; using (var dbContext = new MessangerModel()) { var user = dbContext.Users.FirstOrDefault(u => u.Email == email && u.Password == password); if (user == null) { return(Tuple.Create(CommandHelper.GetCommandResultData(CommandTypes.UserInfo, StatusCodes.BadRequest, new JObject { { "Message", "There is user does not exist!" } }), Guid.Empty)); } return(Tuple.Create(CommandHelper.GetCommandResultData(CommandTypes.UserInfo, StatusCodes.OK, new JObject { { "UserId", user.Id }, { "UserName", user.Name }, { "UserEmail", user.Email } }), user.Id)); } }
public static CommandModel GetContacts(JObject param) { var token = param["Token"].ToString(); var userId = Guid.Parse(param["UserId"].ToString()); string authData = Encoding.UTF8.GetString(Convert.FromBase64String(token)); string[] tokenParts = authData.Split(':'); var email = tokenParts[0]; var password = tokenParts[1]; using (var dbContext = new MessangerModel()) { var user = dbContext.Users.FirstOrDefault(u => u.Email == email && u.Password == password); if (user == null) { return(CommandHelper.GetCommandResultData(CommandTypes.GetContacts, StatusCodes.BadRequest, new JObject { { "Message", "There is user does not exist!" } })); } var userContacts = dbContext.Contacts.Where(contact => contact.User1 == userId || contact.User2 == userId) .Take(500) .ToList(); var contacts = new List <ContactItem>(); contacts.AddRange(userContacts.Select(c => ContactItem.Copy(c, user.Id))); var result = JArray.Parse(JsonConvert.SerializeObject(contacts)); return(CommandHelper.GetCommandResultData(CommandTypes.GetContacts, StatusCodes.OK, result)); } }
public static User GetUserFromContact(Contact compared, Guid currentUserId) { using (var dbContext = new MessangerModel()) { var userFromContact = dbContext.Users.FirstOrDefault(user => (user.Id == compared.User1 || user.Id == compared.User2) && user.Id != currentUserId); return(userFromContact); } }
/// <summary> /// Constructor that creates object of Model, Command, DispatacherTimer, Timespan /// </summary> public MessengerViewModel() { count = 0; key = ""; space = " "; msgCommand = new MessengerCommand(this); objMM = new MessangerModel(); disptimer = new DispatcherTimer(); disptimer.Tick += new EventHandler(disptimer_tick); disptimer.Interval = new TimeSpan(0, 0, 0, 0, 1500); }
public ActionResult Index(string id, int?pageIndex, MessangerModel model) { if (id == this.User.Identity.GetUserId() || !this.userService.CheckIfFriends(this.User.Identity.GetUserId(), id)) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Bad request")); } this.messangerService.Create(User.Identity.GetUserId(), id, model.MessageText); return(RedirectToAction(nameof(Index), new { id, pageIndex })); }
public IActionResult Index(string id, int?pageIndex, MessangerModel model) { if (id == this.User.GetUserId() || !this.userService.CheckIfFriends(this.User.GetUserId(), id)) { return(BadRequest()); } this.messangerService.Create(User.GetUserId(), id, model.MessageText); return(RedirectToAction(nameof(Index), new { id, pageIndex })); }
public static bool IsUserFriend(User compared, Guid currentUserId) { using (var dbContext = new MessangerModel()) { if (dbContext.Contacts.Any(c => (c.FirstUser.Id == currentUserId && c.SecondUser.Id == compared.Id) || (c.FirstUser.Id == compared.Id && c.SecondUser.Id == currentUserId))) { return(true); } } return(false); }
public static bool IsSendedToApprove(User compared, Guid currentUserId) { using (var dbContext = new MessangerModel()) { var contactUser = dbContext.Contacts.FirstOrDefault(cu => cu.User1 == compared.Id && cu.User2 == currentUserId); if (contactUser != null) { return(true); } return(false); } }
public static bool TryRegisterUser(JObject registerData, out CommandModel result) { try { string name = registerData["Name"].ToString(); string email = registerData["Email"].ToString(); string password = registerData["Password"].ToString(); if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(email)) { result = CommandHelper.GetCommandResultData(CommandTypes.Registration, StatusCodes.BadRequest, new JObject { { "Message", "Not all required fields are filled" } }); return(false); } using (var dbContext = new MessangerModel()) { if (dbContext.Users.FirstOrDefault(u => u.Email == email) != null) { result = CommandHelper.GetCommandResultData(CommandTypes.Registration, StatusCodes.BadRequest, new JObject { { "Message", "There is user with same email exist!" } }); return(false); } dbContext.Users.Add(new User { Id = Guid.NewGuid(), Email = email, Name = name, Password = password }); dbContext.SaveChanges(); } result = CommandHelper.GetCommandResultData(CommandTypes.Registration, StatusCodes.OK, new JObject { { "Message", "OK" } }); return(true); } catch (Exception ex) { result = CommandHelper.GetCommandResultData(CommandTypes.Registration, StatusCodes.BadRequest, new JObject { { "Message", ex.Message } }); return(false); } }
public static bool TryAuthorizationUser(JObject authData, out CommandModel result) { try { string email = authData["Email"].ToString(); string password = authData["Password"].ToString(); using (var dbContext = new MessangerModel()) { if (string.IsNullOrEmpty(email)) { result = CommandHelper.GetCommandResultData(CommandTypes.Authorization, StatusCodes.BadRequest, new JObject { { "Message", "Email could not be empty!" } }); return(false); } var user = dbContext.Users.FirstOrDefault(u => u.Email == email && u.Password == password); if (user == null) { result = CommandHelper.GetCommandResultData(CommandTypes.Authorization, StatusCodes.BadRequest, new JObject { { "Message", "There is no user with such email or password" } }); return(false); } // generate and send back user auth token var plainTextBytes = Encoding.UTF8.GetBytes($"{user.Email}:{user.Password}"); var token = Convert.ToBase64String(plainTextBytes); result = CommandHelper.GetCommandResultData(CommandTypes.Authorization, StatusCodes.OK, new JObject { { "Message", "OK" }, { "Token", token } }); return(true); } } catch (Exception ex) { result = CommandHelper.GetCommandResultData(CommandTypes.Authorization, StatusCodes.BadRequest, new JObject { { "Message", ex.Message } }); return(false); } }
public ActionResult Index(string id) { if (id == this.User.Identity.GetUserId() || !this.userService.CheckIfFriends(this.User.Identity.GetUserId(), id)) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Bad request")); } var messangerModel = new MessangerModel(); string compositeChatId = string.Compare(User.Identity.GetUserId(), id) > 0 ? User.Identity.GetUserId() + id : id + User.Identity.GetUserId(); ViewData[GlobalConstants.CompsiteChatId] = compositeChatId; ViewData[GlobalConstants.CounterPartFullName] = this.userService.GetUserFullName(id); messangerModel.Messages = this.messangerService.AllByUserIds(User.Identity.GetUserId(), id); return(View(messangerModel)); }
public IActionResult Index(string id, int?pageIndex) { if (id == this.User.GetUserId() || !this.userService.CheckIfFriends(this.User.GetUserId(), id)) { return(BadRequest()); } var messangerModel = new MessangerModel(); string compositeChatId = string.Compare(User.GetUserId(), id) > 0 ? User.GetUserId() + id : id + User.GetUserId(); ViewData[GlobalConstants.CompsiteChatId] = compositeChatId; ViewData[GlobalConstants.CounterPartFullName] = this.userService.GetUserFullName(id); messangerModel.Messages = this.messangerService.AllByUserIds(User.GetUserId(), id, pageIndex ?? 1, PageSize); return(this.ViewOrNotFound(messangerModel)); }
public static Tuple <CommandModel, Guid> ApproveContact(JObject param) { var token = param["Token"].ToString(); var initiatorId = Guid.Parse(param["InitiatorId"].ToString()); var targetId = Guid.Parse(param["TargetId"].ToString()); string authData = Encoding.UTF8.GetString(Convert.FromBase64String(token)); string[] tokenParts = authData.Split(':'); var email = tokenParts[0]; var password = tokenParts[1]; using (var dbContext = new MessangerModel()) { var user = dbContext.Users.FirstOrDefault(u => u.Email == email && u.Password == password); if (user == null) { return(Tuple.Create(CommandHelper.GetCommandResultData(CommandTypes.ApproveContact, StatusCodes.BadRequest, new JObject { { "Message", "There is user does not exist!" } }), Guid.Empty)); } var contact = dbContext.Contacts.FirstOrDefault(c => (c.User1 == initiatorId && c.User2 == targetId) || (c.User2 == initiatorId && c.User1 == targetId)); if (contact == null) { return(Tuple.Create(CommandHelper.GetCommandResultData(CommandTypes.ApproveContact, StatusCodes.BadRequest, new JObject { { "Message", "There is contact does not exist!" } }), Guid.Empty)); } contact.IsApproved = true; dbContext.SaveChanges(); JObject response = new JObject { { "TargetId", targetId } }; return(Tuple.Create(CommandHelper.GetCommandResultData(CommandTypes.ApproveContact, StatusCodes.OK, response), targetId)); } }
public static CommandModel GetSearchResults(JObject param) { var token = param["Token"].ToString(); var filter = param["Filter"].ToString(); string authData = Encoding.UTF8.GetString(Convert.FromBase64String(token)); string[] tokenParts = authData.Split(':'); var email = tokenParts[0]; var password = tokenParts[1]; using (var dbContext = new MessangerModel()) { var user = dbContext.Users.FirstOrDefault(u => u.Email == email && u.Password == password); if (user == null) { return(CommandHelper.GetCommandResultData(CommandTypes.Search, StatusCodes.BadRequest, new JObject { { "Message", "There is user does not exist!" } })); } var filteredUsers = dbContext.Users.Where(x => x.Id != user.Id && (x.Name.Contains(filter) || x.Email.Contains(filter))) .OrderBy(x => x.Name) .Take(500) .ToList(); var searchItems = new List <SearchItem>(); searchItems.AddRange(filteredUsers.Select(u => SearchItem.Copy(u, user.Id))); var result = JArray.Parse(JsonConvert.SerializeObject(searchItems)); return(CommandHelper.GetCommandResultData(CommandTypes.Search, StatusCodes.OK, result)); } }
internal GenericRepository(MessangerModel context) { this.context = context; this.dbSet = context.Set <TEntity>(); }
public MessengerViewModel() { _model = new MessangerModel(); _model.PropertyChanged += (s, e) => RaisePropertiesChanged(e.PropertyName); }