public async Task CreateComment(HttpContext context) { var parentName = context.Request.Path.Value.Split('/').LastOrDefault(); var folderName = parentName.Remove(parentName.Length - 4, 4) + "_comments"; var time = DateTime.Now; var imageString = ""; var formFiles = context.Request.Form.Files; if (formFiles.Count != 0) { if (formFiles[0].Length > 0) { await using var ms = new MemoryStream(); formFiles[0].CopyTo(ms); var fileBytes = ms.ToArray(); imageString = Convert.ToBase64String(fileBytes); } } var comment = new Comment( context.Request.Form["username"], context.Request.Form["messagename"], context.Request.Form["text"], time, imageString, folderName); var valResults = Validation.Validation.Validate(comment); if (valResults.Any(result => !result.IsValid)) { await context.Response.WriteAsync($@" <!DOCTYPE html> <html> <head> <meta charset='utf-8' /> <title></title> </head> <body> <form action='/Comment/{parentName}' method='post' enctype='multipart/form-data'> {valResults[0].ErrMsg} <br /> Username: <input name=username /> <br /> {valResults[1].ErrMsg} <br /> Message Name: <input name='messagename' /> <br /> Message Text: <textarea name='text'></textarea> <br /> Image: <input name='image' type='file' accept='image/*'/> <br /> <input type='submit' value='Post'/> <br /> </form> </body> </html>" ); return; } _storage.Save(comment); await context.Response.WriteAsync(ReturnPageMaker.Make("New Comment was added")); }
public async Task AddEntry(HttpContext context) { var time = DateTime.Now; var imageString = ""; var formFiles = context.Request.Form.Files; if (formFiles.Count != 0) { if (formFiles[0].Length > 0) { await using var ms = new MemoryStream(); formFiles[0].CopyTo(ms); var fileBytes = ms.ToArray(); imageString = Convert.ToBase64String(fileBytes); } } var message = new Message( context.Request.Form["username"], context.Request.Form["messagename"], context.Request.Form["text"], time, imageString); var valResults = Validation.Validation.Validate(message); if (valResults.Any(result => !result.IsValid)) { await context.Response.WriteAsync($@" <!DOCTYPE html> <html> <head> <meta charset='utf-8' /> <title></title> </head> <body> <form action='/PostsList/'> <input type='submit' value='View messages' /> </form> <form action='/Home/AddEntry/' method='post' enctype='multipart/form-data'> {valResults[0].ErrMsg} <br /> Username: <input name=username /> <br /> {valResults[1].ErrMsg} <br /> Message Name: <input name='messagename' /> <br /> Message Text: <textarea name='text'></textarea> <br /> Image: <input name='image' type='file' accept='image/*'/> <br /> <input type='submit' value='Post'/> <br /> </form> </body> </html>" ); return; } _storage.Save(message); await context.Response.WriteAsync(ReturnPageMaker.Make("New Entry was added")); }
public async Task DeleteMessage(HttpContext context) { var name = context.Request.Path.Value.Split('/').LastOrDefault(); var message = _storage .LoadAll() .FirstOrDefault(m => m.GetCsvName() == name); _storage.Delete(message); await context.Response.WriteAsync(ReturnPageMaker.Make("Entry was deleted")); }
public async Task EditMessage(HttpContext context) { var name = context.Request.Path.Value.Split('/').LastOrDefault(); var message = _storage .LoadAll() .FirstOrDefault(m => m.GetCsvName() == name); Message newMessage; var files = context.Request.Form.Files; if (files.Count > 0) { var imageString = ""; if (files[0].Length > 0) { await using var ms = new MemoryStream(); files[0].CopyTo(ms); var fileBytes = ms.ToArray(); imageString = Convert.ToBase64String(fileBytes); } newMessage = new Message(message.Username, context.Request.Form["messagename"], context.Request.Form["text"], message.Date, imageString); } else { newMessage = new Message(message.Username, context.Request.Form["messagename"], context.Request.Form["text"], message.Date, message.Image); } var valResults = Validation.Validation.Validate(newMessage); if (valResults.Any(result => !result.IsValid)) { await context.Response.WriteAsync($@" <!DOCTYPE html> <html> <head> <meta charset='utf-8' /> <title></title> </head> <body> <form action='/Edit/{name}' method='post' enctype='multipart/form-data'> Username: {message.Username} <br /> {valResults[1].ErrMsg} <br /> Message Name: <input name='messagename' value='{message.MessageName}' /> <br /> Message Text: <textarea name='text'>{message.Text}</textarea> <br /> Image: <input name='image' type='file' accept='image/*'/> <br /> <input type='submit' value='Post'/> <br /> </form> </body> </html>" ); return; } _storage.Save(newMessage); await context.Response.WriteAsync(ReturnPageMaker.Make("Entry was edited")); }