Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ namespace Know_Your_Nation_Speedy.Controllers
[ApiController]
public class UsersController : ControllerBase
{
private readonly MyDbContext _db;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WS

EmailService emailService = new EmailService();
private MyDbContext _db;
readonly IConfiguration _config;
public UsersController(MyDbContext context, IConfiguration config)
{
_db = context;
_config = config;
}

// GET api/values
[HttpGet]
public async Task<ActionResult<IEnumerable<User>>> Get()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did this even compile?

{
return await _db.UserEntries.ToListAsync();
}

[HttpGet("{id}")]
public async Task<IActionResult> GetEntry([FromRoute] int id)
{
Expand All @@ -38,7 +38,6 @@ public async Task<IActionResult> GetEntry([FromRoute] int id)
await _db.SaveChangesAsync();
return Ok(entry);
}

[HttpPost("login")]
public ActionResult<User> Login([FromBody] User User)
{
Expand All @@ -56,22 +55,19 @@ public ActionResult<User> Login([FromBody] User User)
return BadRequest();
}
}

[HttpPost]
public async Task Post([FromBody] User User)
{
await _db.UserEntries.AddAsync(User);
await _db.SaveChangesAsync();
}

[HttpPut("{id}")]
public async Task Put(int id, [FromBody] User User)
{
var entry = await _db.UserEntries.FindAsync(id);
entry = User;
await _db.SaveChangesAsync();
}

[HttpDelete("{id}")]
public async Task<IActionResult> DeleteEntry([FromRoute]int id)
{
Expand All @@ -84,5 +80,27 @@ public async Task<IActionResult> DeleteEntry([FromRoute]int id)
await _db.SaveChangesAsync();
return Ok(entry);
}
[HttpPut()]
[Route("ForgotPassword/{mail}")]
public async Task getCodes(string mail)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function name is not consistent with the standards of the rest of the project and C# as a whole

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, why call it getCodes when in fact it sends an email?

{
string code = emailService.generateCode();
var entry = await _db.UserEntries.FindAsync(mail);

if (entry != null)
{
emailService.SendMail(mail, "testing", code);
}
}
// PUT api/values/5
[HttpPut()]
[Route("ResetPassword/{password} + {mail}")]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the + ?? it has to be a /

public async Task ResetPassword(string mail,string password)
{
var entry = await _db.UserEntries.SingleOrDefaultAsync(m => m.Email == mail);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need a null check and error handling here

entry.Password = password;
_db.UserEntries.Update(entry);
await _db.SaveChangesAsync();
}
}
}
54 changes: 54 additions & 0 deletions Know-Your-Nation-Speedy/Know-Your-Nation-Speedy/EmailService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Mail;
using System.Linq;
using Know_Your_Nation_Speedy.Models;


namespace Know_Your_Nation_Speedy.Models{
public class EmailService
{
string smtpAddress = "smtp.gmail.com";
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of these fields should be in the config

int portNumber = 587;
bool enableSSL = true;
string emailFromAddress = "**********"; //Sender Email Address
string password = "***********"; //Sender Password

public bool SendMail(string To, string Subject)
{
try
{
using (MailMessage mail = new MailMessage())
{
MailAssignment(mail, emailFromAddress, To, Subject, "<a href = 'http://ereader.retrotest.co.za/resetPassword'>Reset Password</h1>");
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't use the generated code here?

SmtpSend(mail);
}
}
catch(Exception e)
{
Console.WriteLine(e.Message);
return false;
}
return true;
}

public void MailAssignment(MailMessage mailMessage, string From, string To, string Subject, string Body)
{
mailMessage.From = new MailAddress(From);
mailMessage.To.Add(To);
mailMessage.Subject = Subject;
mailMessage.IsBodyHtml = true;
mailMessage.Body = Body;
}

public void SmtpSend(MailMessage mail)
{
SmtpClient smtp = new SmtpClient(smtpAddress, portNumber);
smtp.Credentials = new NetworkCredential(emailFromAddress, password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.0" />
<PackageReference Include="Moq" Version="4.10.1" />
<PackageReference Include="MSTest.TestFramework" Version="1.4.0" />
<PackageReference Include="NUnit" Version="3.11.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"Default": "Warning"
}
},
"ConnectionStrings": {
"connection": "Server=dev.retrotest.co.za;Database=ereader;User Id=group4;Password=3bHNuE8&rvG+99U2;"
}
}
"ConnectionStrings": {
"connection": "Server=dev.retrotest.co.za;Database=ereader;User Id=group4;Password=*********;"
}
}