Skip to content

Commit

Permalink
Use zipcodes in emails (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShroomAgent27 authored Mar 21, 2024
1 parent 0b4ed62 commit 9139d96
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 16 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ obj/
/wwwroot/node_modules

# End of https://www.toptal.com/developers/gitignore/api/dotnetcore

build.bat
35 changes: 28 additions & 7 deletions AzureHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,47 +17,68 @@ 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<string> GetEmails()
public List<Email> GetEmails()
{
return GetEmails0().Result;

}

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<List<string>> GetEmails0()
private async Task<List<Email>> GetEmails0()
{
string localFilePath = GetLocalFilePath();

await bc.DownloadToAsync(localFilePath);

string json = await File.ReadAllTextAsync(localFilePath);

return JsonConvert.DeserializeObject<List<string>>(json);
return JsonConvert.DeserializeObject<List<Email>>(json);
}

private async void SaveEmails(List<string> emails)
private async void SaveEmails(List<Email> emails)
{
string localFilePath = GetLocalFilePath();

Expand Down
9 changes: 6 additions & 3 deletions Email.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
5 changes: 0 additions & 5 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
9 changes: 8 additions & 1 deletion WebHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
})
Expand Down Expand Up @@ -95,7 +102,7 @@ private async Task<Email> 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;
Expand Down

0 comments on commit 9139d96

Please sign in to comment.