public async Task <Chatroom> CreateAsync(Chatroom dto, ApplicationUser user, Action <string, string> AddErrorMessage) { if (user == null) { AddErrorMessage?.Invoke("General", "Only Logged-in user can perform this operation"); return(null); } ChatroomEntity entity = _mappingService.DtoToEntity(dto); entity.OwnerID = user.Id; ChatroomEntity createdEntity = await _repository.CreateAsync(entity); if (createdEntity != null) { await _context.SaveChangesAsync(); } createdEntity = await _repository.GetCompleteAsync(createdEntity.ID); Chatroom created = _mappingService.EntityToDto(createdEntity); created.IsOwner = true; return(created); }
public async Task <Chatroom> UpdateAsync(Chatroom dto, ApplicationUser user, Action <string, string> AddErrorMessage) { ChatroomEntity updatedEntity = await _repository.SingleOrDefaultAsync(ab => ab.ID == dto.ID); if (updatedEntity == null) { // Use Dto for catching by Controller throw new EntityNotFoundException <Chatroom, int>(dto.ID); } _context.Entry(updatedEntity).State = EntityState.Detached; if (!CheckOwner(updatedEntity, user, AddErrorMessage)) { return(null); } updatedEntity.Title = dto.Title; updatedEntity = _repository.Update(updatedEntity); if (updatedEntity != null) { await _context.SaveChangesAsync(); } updatedEntity = await _repository.GetCompleteAsync(updatedEntity.ID); return(_mappingService.EntityToDto(updatedEntity)); }
public int Create(ChatroomEntity chatroom) { if (chatroom == null) { throw new ArgumentNullException(nameof(chatroom)); } chatroom.IsActive = true; var dto = mapper.MapFromEntityToDto(chatroom); return(Convert.ToInt32(chatroomRepository.Insert(dto))); }
public ChatroomViewModel MapFromEntityToModel(ChatroomEntity entity) { return(new ChatroomViewModel() { ChatroomId = entity.ChatroomId, Name = entity.Name, Description = entity.Description, Owner = entity.Owner, IsActive = entity.IsActive }); }
public void GetById_WhenIdDoesNotExists_ReturnsNotFoundStatus(int id) { ChatroomEntity chatRoomEntity = null; chatroomBusiness.Setup(x => x.GetById(id)).Returns(chatRoomEntity); chatroomController.Request = new HttpRequestMessage(); chatroomController.Configuration = new HttpConfiguration(); var response = chatroomController.GetById(id); Assert.IsTrue(response.StatusCode == HttpStatusCode.NotFound); }
private bool CheckOwner(ChatroomEntity entity, ApplicationUser user, Action <string, string> AddErrorMessage) { if (user == null) { AddErrorMessage?.Invoke("General", "Only Logged-in user can perform this operation"); return(false); } if (user.Id != entity.OwnerID) { AddErrorMessage?.Invoke("General", "Only Owner of a Chatroom can perform this operation!"); return(false); } return(true); }
public void Initialize() { module = new UsersModule(new TestUserHandler(), MakeConfig()); Admin = new ChatroomEntity(AdminIdentifier, null); Admin.Rank = ChatroomEntity.AdminStatus.True; NonAdmin = new ChatroomEntity(NonAdminIdentifier, null); NonAdmin.Rank = ChatroomEntity.AdminStatus.False; UnknownUser = new ChatroomEntity(UnknownUserIdentifier, null); UnknownUser.Rank = ChatroomEntity.AdminStatus.Unknown; OtherUser = new ChatroomEntity(OtherUserIdentifier, null); OtherUser.Rank = ChatroomEntity.AdminStatus.Other; AdminDeterminedByConfig = new ChatroomEntity(AdminDeterminedByConfigIdentifier, null); AdminDeterminedByConfig.Rank = ChatroomEntity.AdminStatus.False; }
public async Task <Chatroom> DeleteAsync(int id, ApplicationUser user, Action <string, string> AddErrorMessage) { ChatroomEntity deletedEntity = await _repository.SingleOrDefaultAsync(p => p.ID == id); if (deletedEntity == null) { // Use Dto for catching by Controller throw new EntityNotFoundException <Chatroom, int>(id); } if (!CheckOwner(deletedEntity, user, AddErrorMessage)) { return(null); } if (_repository.Remove(deletedEntity) != null) { await _context.SaveChangesAsync(); } return(_mappingService.EntityToDto(deletedEntity)); }
public List <MessageEventArgs> GetChannelMessages(string channel) { System.Threading.Thread.Sleep(125); var client = new RestClient("https://discordapp.com/api/v6/channels/" + channel + "/messages?after=" + Last_Message[channel]); var request = new RestRequest(Method.GET); request.AddHeader("postman-token", "ad8e82e8-c4bc-33b2-b891-6ab7d35a0fda"); request.AddHeader("cache-control", "no-cache"); request.AddHeader("authorization", "Bot " + token); IRestResponse response = client.Execute(request); List <MessageEventArgs> Returning = new List <MessageEventArgs>(); List <ChannelMessages> ChannelMessages = JsonConvert.DeserializeObject <List <ChannelMessages> >(response.Content); if (ChannelMessages.Count > 0) { Last_Message[channel] = ChannelMessages[0].id; } foreach (ChannelMessages msg in ChannelMessages) { MessageEventArgs Converted_msg = new MessageEventArgs(this); Converted_msg.Chatroom = new ChatroomEntity(channel, this); Converted_msg.ReceivedMessage = msg.content; var sender = new ChatroomEntity(msg.author.id, this); sender.DisplayName = msg.author.username; sender.ParentIdentifier = channel; Converted_msg.Sender = sender; if (CheckUserIsAdmin(msg.author.id)) { sender.Rank = ChatroomEntity.AdminStatus.True; } Returning.Add(Converted_msg); } return(Returning); }
public async Task <Chatroom> GetAsync(int id) { ChatroomEntity entity = await _repository.GetCompleteAsync(id); return(_mappingService.EntityToDto(entity)); }
public override void ReceiveChatMemberInfo(ChatroomEntity ChatroomEntity, bool AdminStatus) { }
public void Initialize() { module = new ServerTrackingModule(new TestUserHandler(), new TestUserHandler(), MakeConfig()); TestUser = new User(0, null); TestUser.Rank = ChatroomEntity.AdminStatus.True; }
public override string GetOthersUsername(object sender, ChatroomEntity user) { return("N/A"); }
public override void EnterChatRoom(object sender, ChatroomEntity ChatroomEntity) { }
public void Update(ChatroomEntity chatroom) { var chatroomDto = mapper.MapFromEntityToDto(chatroom); chatroomRepository.Update(chatroomDto); }
public void Initialize() { module = new MapModule(new TestUserHandler(), new TestUserHandler(), MakeConfig()); TestUser = new User(identifier, null); module = new MapModule(new TestUserHandler(), new TestUserHandler(), MakeConfig()); }
public SyntaxUnitTests() { TestUser = new User(identifier, null); module = new MapModule(new TestUserHandler(), new TestUserHandler(), MakeConfig()); }
public override void LeaveChatroom(object sender, ChatroomEntity ChatroomEntity) { }