public static async Task <List <Comment> > HandleViewComments(Client client, Photo photo) { ProtocolHelpers.SendRequestCommand(ProtocolConstants.RequestCommands.VIEW_COMMENTS, client.StreamCommunication); client.StreamCommunication.Write(ConversionHandler.ConvertLongToBytes(photo.Id)); ConversionHandler.ConvertBytesToShort(await client.StreamCommunication.ReadAsync(ProtocolConstants.ShortTypeLength)); ConversionHandler.ConvertBytesToShort(await client.StreamCommunication.ReadAsync(ProtocolConstants.ShortTypeLength)); var data = await client.StreamCommunication.ReadAsync(ProtocolConstants.IntegerTypeLength); var dataLength = ConversionHandler.ConvertBytesToInt(data); var result = new List <Comment>(); while (dataLength != 0) { var name = ConversionHandler.ConvertBytesToString(await client.StreamCommunication.ReadAsync(User.UserNameLength)); var email = ConversionHandler.ConvertBytesToString(await client.StreamCommunication.ReadAsync(User.UserEmailLength)); var message = ConversionHandler.ConvertBytesToString(await client.StreamCommunication.ReadAsync(Comment.CommentLength)); dataLength -= User.UserNameLength + User.UserEmailLength + Comment.CommentLength; result.Add(new Comment() { Commentator = new User { Email = email, Name = name }, Message = message, }); } return(result); }
public static async Task <List <User> > HandleViewUsers(Client client) { ProtocolHelpers.SendRequestCommand(ProtocolConstants.RequestCommands.VIEW_USERS, client.StreamCommunication); ConversionHandler.ConvertBytesToShort(await client.StreamCommunication.ReadAsync(ProtocolConstants.ShortTypeLength)); ConversionHandler.ConvertBytesToShort(await client.StreamCommunication.ReadAsync(ProtocolConstants.ShortTypeLength)); var data = await client.StreamCommunication.ReadAsync(ProtocolConstants.IntegerTypeLength); var dataLength = ConversionHandler.ConvertBytesToInt(data); var result = new List <User>(); while (dataLength != 0) { var name = ConversionHandler.ConvertBytesToString(await client.StreamCommunication.ReadAsync(User.UserNameLength)); var email = ConversionHandler.ConvertBytesToString(await client.StreamCommunication.ReadAsync(User.UserEmailLength)); var lastConnectionDate = ConversionHandler.ConvertBytesToString(await client.StreamCommunication.ReadAsync(ProtocolConstants.DateTimeTypeLength)); dataLength -= User.UserNameLength + User.UserEmailLength + ProtocolConstants.DateTimeTypeLength; result.Add(new User { Name = name, Email = email, //LastConnection = DateTime.Parse(lastConnectionDate) }); } return(result); }
public static async Task HandleUploadPhoto(Server server, CommunicationClient client) { var name = ConversionHandler.ConvertBytesToString(await client.StreamCommunication.ReadAsync(Photo.PhotoNameLength)); var extension = ConversionHandler.ConvertBytesToString(await client.StreamCommunication.ReadAsync(Photo.PhotoExtensionLength)); var fileSize = ConversionHandler.ConvertBytesToLong(await client.StreamCommunication.ReadAsync(ProtocolConstants.LongTypeLength)); if (string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(extension)) { ProtocolHelpers.SendMessageCommand(ProtocolConstants.ResponseCommands.Ok, client, "Input Error"); } var photo = new PhotoDto() { Name = name, Extension = extension, FileSize = fileSize, UserEmail = client.User.Email }; await server.Service.UploadPhotoAsync(photo); var fileName = $"{PhotosPath}\\Image_{photo.Id}{extension}"; await FileHandler.ReceiveFileWithStreams(fileSize, fileName, client.StreamCommunication); loggerService.SendMessages("Image uploaded"); ProtocolHelpers.SendMessageCommand(ProtocolConstants.ResponseCommands.Ok, client, "Added succesfully"); }
public static async Task <bool> ValidateLogin(Server server, CommunicationClient client) { var email = ConversionHandler.ConvertBytesToString(await client.StreamCommunication.ReadAsync(User.UserEmailLength)); var password = ConversionHandler.ConvertBytesToString(await client.StreamCommunication.ReadAsync(User.UserPasswordLength)); var user = new UserDto() { Email = email, Password = password }; var existUser = await server.Service.AutenticateUserAsync(user); if (!existUser) { ProtocolHelpers.SendResponseCommand(ProtocolConstants.ResponseCommands.Error, client.StreamCommunication); client.StreamCommunication.Write(ConversionHandler.ConvertStringToBytes("Invalid User", ProtocolConstants.ResponseMessageLength)); } else { client.User = new User() { Email = email, Password = password }; loggerService.SendMessages("Login Successfully, mail: " + email); ProtocolHelpers.SendResponseCommand(ProtocolConstants.ResponseCommands.Ok, client.StreamCommunication); client.StreamCommunication.Write(ConversionHandler.ConvertStringToBytes("Login Successfully", ProtocolConstants.ResponseMessageLength)); } return(existUser); }
public static async Task <bool> HandleCreateUser(Server server, CommunicationClient client) { var name = ConversionHandler.ConvertBytesToString(await client.StreamCommunication.ReadAsync(User.UserNameLength)); var email = ConversionHandler.ConvertBytesToString(await client.StreamCommunication.ReadAsync(User.UserEmailLength)); var password = ConversionHandler.ConvertBytesToString(await client.StreamCommunication.ReadAsync(User.UserPasswordLength)); if (string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(email)) { ProtocolHelpers.SendResponseCommand(ProtocolConstants.ResponseCommands.Error, client.StreamCommunication); client.StreamCommunication.Write(ConversionHandler.ConvertStringToBytes("Input Error", ProtocolConstants.ResponseMessageLength)); } var user = new UserDto() { Email = email, Name = name, Password = password, IsLogedIn = true }; var response = await server.Service.AddUserAsync(user); if (response.Status.Equals("Ok")) { client.User = new User() { Email = email, Name = name, Password = password, }; loggerService.SendMessages("User created, mail: " + email); ProtocolHelpers.SendResponseCommand(ProtocolConstants.ResponseCommands.Ok, client.StreamCommunication); client.StreamCommunication.Write(ConversionHandler.ConvertStringToBytes(response.Message, ProtocolConstants.ResponseMessageLength)); } else { ProtocolHelpers.SendResponseCommand(ProtocolConstants.ResponseCommands.Error, client.StreamCommunication); client.StreamCommunication.Write(ConversionHandler.ConvertStringToBytes(response.Message, ProtocolConstants.ResponseMessageLength)); } return(response.Status.Equals("Ok")); }
public static async Task HandleCommentPhoto(Server server, CommunicationClient client) { var photoIdParsed = ConversionHandler.ConvertBytesToLong(await client.StreamCommunication.ReadAsync(ProtocolConstants.LongTypeLength)); var message = ConversionHandler.ConvertBytesToString(await client.StreamCommunication.ReadAsync(Comment.CommentLength)); if (string.IsNullOrWhiteSpace(message)) { ProtocolHelpers.SendMessageCommand(ProtocolConstants.ResponseCommands.Ok, client, "Input Error"); } var comment = new CommentDto() { PhotoId = photoIdParsed, Message = message, UserEmail = client.User.Email, }; await server.Service.AddCommentAsync(comment); loggerService.SendMessages("Image commented"); ProtocolHelpers.SendMessageCommand(ProtocolConstants.ResponseCommands.Ok, client, "Added Sucessfully"); }
public static async Task <List <Photo> > HandleViewPhotos(Client client) { ProtocolHelpers.SendRequestCommand(ProtocolConstants.RequestCommands.VIEW_PHOTOS, client.StreamCommunication); var response = ConversionHandler.ConvertBytesToShort(await client.StreamCommunication.ReadAsync(ProtocolConstants.ShortTypeLength)); var responseCommand = ConversionHandler.ConvertBytesToShort(await client.StreamCommunication.ReadAsync(ProtocolConstants.ShortTypeLength)); var data = await client.StreamCommunication.ReadAsync(ProtocolConstants.IntegerTypeLength); var dataLength = ConversionHandler.ConvertBytesToInt(data); var result = new List <Photo>(); while (dataLength != 0) { var email = ConversionHandler.ConvertBytesToString(await client.StreamCommunication.ReadAsync(User.UserEmailLength)); var photoId = ConversionHandler.ConvertBytesToLong(await client.StreamCommunication.ReadAsync(ProtocolConstants.LongTypeLength)); var photoName = ConversionHandler.ConvertBytesToString(await client.StreamCommunication.ReadAsync(Photo.PhotoNameLength)); var extension = ConversionHandler.ConvertBytesToString(await client.StreamCommunication.ReadAsync(Photo.PhotoExtensionLength)); var photoLength = ConversionHandler.ConvertBytesToLong(await client.StreamCommunication.ReadAsync(ProtocolConstants.LongTypeLength)); dataLength -= User.UserEmailLength + ProtocolConstants.LongTypeLength + Photo.PhotoNameLength + Photo.PhotoExtensionLength + ProtocolConstants.LongTypeLength; result.Add(new Photo() { User = new User() { Email = email }, Id = photoId, Name = photoName, FileSize = photoLength, Extension = extension, }); } return(result); }