diff --git a/.gitignore b/.gitignore index 25416e0..893bb8a 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,5 @@ obj/ /wwwroot/node_modules # End of https://www.toptal.com/developers/gitignore/api/dotnetcore + +build.bat \ No newline at end of file diff --git a/AzureHandler.cs b/AzureHandler.cs index abba995..37137e8 100644 --- a/AzureHandler.cs +++ b/AzureHandler.cs @@ -17,13 +17,13 @@ public class AzureHandler public AzureHandler() { bsc = new BlobServiceClient( - new Uri("https://weathertrackerb951.blob.core.windows.net"), + new Uri("https://weatherb313.blob.core.windows.net"), new DefaultAzureCredential()); bcc = bsc.GetBlobContainerClient("emails"); bc = bcc.GetBlobClient(FILE_NAME); } - public List GetEmails() + public List GetEmails() { return GetEmails0().Result; @@ -31,22 +31,43 @@ public List GetEmails() public void AddEmail(Email email) { + bool updated = false; var emails = GetEmails(); - emails.Add(email.email); + foreach(Email e in emails) + { + if (e.email == email.email) + { + e.zipcode = email.zipcode; + updated = true; + } + + } + if (!updated) + emails.Add(email); SaveEmails(emails); } public bool DeleteEmail(Email email) { + bool removed = false; var emails = GetEmails(); - bool removed = emails.Remove(email.email); + Email toRemove = null; + foreach(Email e in emails) + { + if (e.email == email.email) + { + toRemove = e; + break; + } + } + removed = emails.Remove(toRemove); if (removed) SaveEmails(emails); return removed; } - private async Task> GetEmails0() + private async Task> GetEmails0() { string localFilePath = GetLocalFilePath(); @@ -54,10 +75,10 @@ private async Task> GetEmails0() string json = await File.ReadAllTextAsync(localFilePath); - return JsonConvert.DeserializeObject>(json); + return JsonConvert.DeserializeObject>(json); } - private async void SaveEmails(List emails) + private async void SaveEmails(List emails) { string localFilePath = GetLocalFilePath(); diff --git a/Email.cs b/Email.cs index 1aeacaf..59a7016 100644 --- a/Email.cs +++ b/Email.cs @@ -4,15 +4,18 @@ public class Email { - public Email(string email) + public Email(string email, int zipcode) { if (new EmailAddressAttribute().IsValid(email)) this.email = email; + this.zipcode = zipcode; } - public string email { get; } + public string email { set; get; } + + public int zipcode { set; get; } public override string ToString() { - return email; + return email + zipcode; } } diff --git a/Program.cs b/Program.cs index de5c431..ea80abd 100644 --- a/Program.cs +++ b/Program.cs @@ -1,8 +1,3 @@ -using System.Net; -using System.Text; -using System.Web.Mvc; -using Newtonsoft.Json; - public class Program { public static void Main(string[] args) diff --git a/WebHandler.cs b/WebHandler.cs index 61581ed..d71beef 100644 --- a/WebHandler.cs +++ b/WebHandler.cs @@ -48,7 +48,14 @@ private void buildWebApp(string[] args) { Email email = await GetEmailAndSetResponse(context.Request, context.Response); if (email is null) + { return; + } + if (email.zipcode == 0) + { + context.Response.StatusCode = (int)HttpStatusCode.BadRequest; + return; + } handler.AddEmail(email); context.Response.StatusCode = (int)HttpStatusCode.OK; }) @@ -95,7 +102,7 @@ private async Task GetEmailAndSetResponse(HttpRequest request, HttpRespon response.StatusCode = (int)HttpStatusCode.BadRequest; return null; } - if (email is null || email.email is null) + if (email is null || String.IsNullOrEmpty(email.email)) { response.StatusCode = (int)HttpStatusCode.BadRequest; return null;