public async Task CreateNewGroupAsyncShould_CreateGroupChatIf3UserIdsAreProvided() { const string databaseName = nameof(CreateNewGroupAsyncShould_CreateGroupChatIf3UserIdsAreProvided); using (var arrangeContext = new WebChatContext(TestUtils.GetOptions(databaseName))) { await arrangeContext.Users.AddAsync(new User { Id = "1", UserName = "******" }); await arrangeContext.Users.AddAsync(new User { Id = "2", UserName = "******" }); await arrangeContext.Users.AddAsync(new User { Id = "3", UserName = "******" }); await arrangeContext.SaveChangesAsync(); } using (var actContext = new WebChatContext(TestUtils.GetOptions(databaseName))) { GroupService sut = new GroupService(actContext); CreateGroupRequest groupRequest = new CreateGroupRequest() { Name = "test", UserIds = { "1", "2", "3" } }; var result = await sut.CreateNewGroupAsync(groupRequest, "1"); Assert.AreEqual("test", result.Name); Assert.AreEqual(false, result.IsPrivateChat); Assert.IsTrue(DateTime.UtcNow - result.LastActivityDate < TimeSpan.FromSeconds(2)); }; }
protected void ButtonSendMessage_Click(object sender, EventArgs e) { string username = this.TextBoxUsername.Text; string message = this.TextBoxMessage.Text; var context = new WebChatContext(); var author = context.Users.FirstOrDefault(u => u.Username == username); if (author == null) { var newUser = new User() { Username = username }; author = context.Users.Add(newUser); context.SaveChanges(); } var newMessage = new Message() { Text = this.TextBoxMessage.Text, Author = author, CreationDate = DateTime.Now }; context.Messages.Add(newMessage); context.SaveChanges(); }
public async Task SaveGroupMessageAsync_SaveMessageToGroup() { const string databaseName = nameof(SaveGroupMessageAsync_SaveMessageToGroup); using (var arrangeContext = new WebChatContext(TestUtils.GetOptions(databaseName))) { await arrangeContext.Users.AddAsync(new User { Id = "1", UserName = "******" }); await arrangeContext.Groups.AddAsync(new Group { Id = 1, Name = "Group1" }); await arrangeContext.UserGroups.AddAsync(new UserGroup { UserId = "1", GroupId = 1 }); await arrangeContext.SaveChangesAsync(); } using (var actContext = new WebChatContext(TestUtils.GetOptions(databaseName))) { var sut = new MessageService(actContext); MessageWithUserData result = await sut.SaveGroupMessageAsync("1", "Message1", 1); Assert.AreEqual(1, result.GroupId); Assert.AreEqual("User1", result.UserName); Assert.AreEqual("1", result.UserId); Assert.AreEqual("Message1", result.Text); Assert.IsTrue(DateTime.UtcNow - DateTime.Parse(result.Date) < TimeSpan.FromSeconds(2)); } }
// GET api/values public int Get() { var ctx = new WebChatContext(); var msgsCount = ctx.Messages.Count(); return(msgsCount); }
public async Task SaveGroupMessageAsync_UpdatesGroupLastActivityDate() { const string databaseName = nameof(SaveGroupMessageAsync_UpdatesGroupLastActivityDate); using (var arrangeContext = new WebChatContext(TestUtils.GetOptions(databaseName))) { await arrangeContext.Users.AddAsync(new User { Id = "1", UserName = "******" }); await arrangeContext.Groups.AddAsync(new Group { Id = 1, Name = "Group1" }); await arrangeContext.UserGroups.AddAsync(new UserGroup { UserId = "1", GroupId = 1 }); await arrangeContext.SaveChangesAsync(); } using (var actContext = new WebChatContext(TestUtils.GetOptions(databaseName))) { var sut = new MessageService(actContext); await sut.SaveGroupMessageAsync("1", "Message1", 1); var result = await actContext.Groups.FirstAsync(g => g.Name == "Group1"); Assert.IsTrue(DateTime.UtcNow - result.LastActivityDate < TimeSpan.FromSeconds(2)); } }
static void Main() { var context = new WebChatContext(); var count = context.Messages.Count(); Console.WriteLine(count); }
public IQueryable <Message> ListViewMessages_GetData() { var context = new WebChatContext(); return(context.Messages .Include(m => m.Author) .OrderBy(m => m.CreationDate)); }
public UserService(WebChatContext context, UserManager <User> userManager, SignInManager <User> signInManager, IJwtGenerator jwtGenerator, IConfiguration configuration, IFileService fileService) { _context = context; _userManager = userManager; _signInManager = signInManager; _jwtGenerator = jwtGenerator; _configuration = configuration; _fileService = fileService; }
private static async Task SeedGroup(WebChatContext context, List <User> users, string name) { int numberOfUsers = users.Count; Group group = new Group() { Name = name, IsPrivateChat = (numberOfUsers == 2), LastActivityDate = DateTime.UtcNow, UserGroups = new List <UserGroup>() }; var firstChatResultUserGroups = await context.Groups.AddAsync(group); var userGroups = new List <UserGroup>(); for (int i = 0; i < numberOfUsers; i++) { userGroups.Add(new UserGroup() { GroupId = group.Id, UserId = users[i % numberOfUsers].Id }); } context.UserGroups.AddRange(userGroups); await context.SaveChangesAsync(); int userIdIndex = 0; for (int i = 1; i <= 20; i++) { if (i < 10) { userIdIndex = i % numberOfUsers; } else if (i <= 15) { userIdIndex = numberOfUsers - 1; } else { userIdIndex = numberOfUsers - 2; } context.Messages.Add(new Message() { GroupId = group.Id, UserId = users[userIdIndex].Id, Text = $"Message {i}", Date = DateTime.UtcNow }); await context.SaveChangesAsync(); } }
public static async Task SeedDataAsync(WebChatContext context, UserManager <User> userManager) { if (!context.Users.Any()) { List <User> users = await SeedUsers(userManager); await SeedPublicGroupAsync(context, users); await SeedGroup(context, users, "First Group"); await SeedGroup(context, users, null); await SeedChat(context, users.Take(2).ToList()); await SeedChat(context, users.Skip(1).Take(2).ToList()); } }
public UserService( WebChatContext ctx, IAuthService authService, IThreadService threadService, IMappingService mappingService, IConnectionMapping <string> connectionMapping) { this.ctx = ctx ?? throw new ArgumentNullException("Context can not be null"); this.authService = authService ?? throw new ArgumentNullException("Authorization service can not be null"); this.threadService = threadService ?? throw new ArgumentNullException("Thread service service can not be null"); this.mappingService = mappingService ?? throw new ArgumentNullException("Mapping service service can not be null"); this.connectionMapping = connectionMapping ?? throw new ArgumentNullException(nameof(connectionMapping)); }
private static async Task SeedPublicGroupAsync(WebChatContext context, List <User> users) { var messages = new List <Message>(); for (int i = 1; i <= 30; i++) { context.Messages.AddRange(new Message() { GroupId = null, UserId = users[(i > 25) ? i % i : 2].Id, Text = $"Public group message {i}", Date = DateTime.UtcNow.AddDays((i > 25) ? 0 : -30 + i) }); await context.SaveChangesAsync(); } }
public async Task SaveGroupMessageAsync_ThrowsIfUserDoesNotBelongToAGroup() { const string databaseName = nameof(SaveGroupMessageAsync_ThrowsIfUserDoesNotBelongToAGroup); using (var arrangeContext = new WebChatContext(TestUtils.GetOptions(databaseName))) { await arrangeContext.Users.AddAsync(new User { Id = "1", UserName = "******" }); await arrangeContext.Groups.AddAsync(new Group { Id = 1, Name = "Group1" }); await arrangeContext.UserGroups.AddAsync(new UserGroup { UserId = "2", GroupId = 1 }); await arrangeContext.SaveChangesAsync(); } using (var actContext = new WebChatContext(TestUtils.GetOptions(databaseName))) { var sut = new MessageService(actContext); await Assert.ThrowsExceptionAsync <GroupAccessException>(() => sut.SaveGroupMessageAsync("1", "Message1", 1)); } }
public ChatController(WebChatContext context, IConfiguration iConfig) { _context = context; _config = iConfig; var options = new PusherOptions(); // options.Cluster = "ap1"; options.Cluster = _config.GetValue <string>("PusherCluster"); /* pusher = new Pusher( * "1186554", * "224a25b62e672be2a092", * "23ab34ee56aceeb0efc2", * options * ); */ pusher = new Pusher( _config.GetValue <string>("PusherAppId"), _config.GetValue <string>("PusherAppKey"), _config.GetValue <string>("PusherAppSecret"), options ); }
public ConversationController(WebChatContext context) { _context = context; }
public HomeController(ILogger <HomeController> logger, WebChatContext context) { _logger = logger; _context = context; }
public static void SeedData(WebChatContext ctx) { Console.WriteLine("Applying migrations..."); ctx.Database.Migrate(); }
public ThreadService(WebChatContext ctx, IMappingService mappingService) { this.ctx = ctx; this.mappingService = mappingService; }
public Validator(IThreadService threadService, WebChatContext ctx) { this.threadService = threadService; this.ctx = ctx; }
public MessengerController(WebChatContext context) { _context = context; }
public BaseApiController(WebChatContext data) { this.Data = data; }
private static async Task SeedChat(WebChatContext context, List <User> users) { await SeedGroup(context, users, null); }
public BaseApiController(WebChatContext data) { this.Data = data; }
public UsersController(WebChatContext context, IConfiguration iConfig) { _context = context; _config = iConfig; }
public MessageService(WebChatContext context) { _context = context; }
public SeedController(WebChatContext ctx) { this.ctx = ctx; }
public UserGroupService(WebChatContext context) { _context = context; }
public ChatRepository(WebChatContext context) { _webChatContext = context; }
public async Task GetUserGroupsAsyncShould_ReturnEmptyUserGroupsListIfNoUserGroupsExist() { const string databaseName = nameof(GetUserGroupsAsyncShould_ReturnEmptyUserGroupsListIfNoUserGroupsExist); using (var arrangeContext = new WebChatContext(TestUtils.GetOptions(databaseName))) { CreateGroupRequest groupRequest = new CreateGroupRequest() { Name = null, UserIds = { "3", "2" } }; var user2 = new User() { Id = "2", UserName = "******" }; var user3 = new User() { Id = "3", UserName = "******" }; await arrangeContext.Users.AddAsync(user2); await arrangeContext.Users.AddAsync(user3); var UserGroup2 = new UserGroup() { UserId = "2", GroupId = 1, User = user2 }; var UserGroup1 = new UserGroup() { UserId = "3", GroupId = 1, User = user3 }; var Group1UserGroups = new List <UserGroup> { UserGroup1, UserGroup2 }; await arrangeContext.UserGroups.AddAsync(UserGroup1); await arrangeContext.UserGroups.AddAsync(UserGroup2); var Group = new Group() { Id = 1, IsPrivateChat = true, LastActivityDate = new DateTime(2019, 5, 5), UserGroups = Group1UserGroups }; await arrangeContext.Groups.AddAsync(Group); arrangeContext.SaveChanges(); } using (var actContext = new WebChatContext(TestUtils.GetOptions(databaseName))) { GroupService sut = new GroupService(actContext); var result = await sut.GetUserGroupsAsync("1"); Assert.AreEqual(0, result.Count); }; }
public MessageService(WebChatContext ctx, IUserService userService, IMappingService mappingService) { this.ctx = ctx; this.userService = userService; this.mappingService = mappingService; }
public async Task CreateNewGroupAsyncShould_ReturntheSameChatIfItAlreadyExists() { const string databaseName = nameof(CreateNewGroupAsyncShould_ReturntheSameChatIfItAlreadyExists); using (var arrangeContext = new WebChatContext(TestUtils.GetOptions(databaseName))) { var user1 = new User() { Id = "1", UserName = "******" }; var user2 = new User() { Id = "2", UserName = "******" }; await arrangeContext.Users.AddAsync(user1); await arrangeContext.Users.AddAsync(user2); var UserGroup1 = new UserGroup() { UserId = "1", GroupId = 1, User = user1 }; var UserGroup2 = new UserGroup() { UserId = "2", GroupId = 1, User = user2 }; var Group1UserGroups = new List <UserGroup>(); Group1UserGroups.Add(UserGroup1); Group1UserGroups.Add(UserGroup2); var oldGroup = new Group() { Id = 1, IsPrivateChat = true, LastActivityDate = new DateTime(2019, 5, 5), UserGroups = Group1UserGroups }; await arrangeContext.Groups.AddAsync(oldGroup); arrangeContext.SaveChanges(); } using (var actContext = new WebChatContext(TestUtils.GetOptions(databaseName))) { GroupService sut = new GroupService(actContext); var groupRequest = new CreateGroupRequest() { Name = null, UserIds = { "1", "2" } }; var result = await sut.CreateNewGroupAsync(groupRequest, "1"); Assert.AreEqual(1, result.Id); Assert.AreEqual(true, result.IsPrivateChat); Assert.AreEqual(new DateTime(2019, 5, 5), result.LastActivityDate); }; }