Skip to content

Instantly share code, notes, and snippets.

@dengsauve
Created November 15, 2017 02:26
Show Gist options
  • Save dengsauve/92bc4cdc56a2e71f2b317d08cf4d4792 to your computer and use it in GitHub Desktop.
Save dengsauve/92bc4cdc56a2e71f2b317d08cf4d4792 to your computer and use it in GitHub Desktop.
Email-Web-Api
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using MvcMovie.Models;
namespace MvcMovie.ApiControllers
{
[Produces("application/json")]
[Route("api/1.0/Emails")]
public class EmailsController : Controller
{
private readonly Context _context;
public EmailsController(Context context)
{
_context = context;
}
// GET: api/1.0/Emails/All
[HttpGet("All")]
public async Task<IActionResult> All()
{
return Ok(await _context.Email.ToListAsync());
}
// POST: api/1.0/Emails/Add
[HttpPost("Add")]
public async Task<IActionResult> AddEmail([FromBody] Email email)
{
if (!ModelState.IsValid)
{
return BadRequest();
}
var studentFound = await _context.Students.FirstOrDefaultAsync(e => e.StudentId == email.StudentId);
if(studentFound == null)
{
return NotFound("Student not found by id");
}
email.EmailAddress = email.EmailAddress.ToLower();
_context.Email.Add(email);
await _context.SaveChangesAsync();
return Ok(email);
}
// PUT: api/1.0/Emails/Update
[HttpPut("Update")]
public async Task<IActionResult> Update([FromBody] Email email)
{
if (!ModelState.IsValid)
{
return BadRequest("Invalid Model Passed");
}
var emailFound = await _context.Email.FirstOrDefaultAsync(e => e.EmailId == email.EmailId);
var studentFound = await _context.Students.FirstOrDefaultAsync(s => s.StudentId == email.StudentId);
if (emailFound == null)
{
return NotFound("No Matching Email Found");
}
if (studentFound == null)
{
return NotFound("No Student Found by id");
}
emailFound.EmailAddress = email.EmailAddress.ToLower();
emailFound.StudentId = email.StudentId;
await _context.SaveChangesAsync();
return NoContent();
}
// DELETE: api/1.0/Emails/Delete/id
[HttpDelete("Delete/{id}")]
public async Task<IActionResult> DeleteCourse([FromRoute] int id)
{
var email = await _context.Email.FirstOrDefaultAsync(c => c.EmailId == id);
if (email == null)
{
return NotFound("Email not found by id");
}
else
{
_context.Email.Remove(email);
await _context.SaveChangesAsync();
return NoContent();
}
}
// GET: /api/1.0/student/{studentId}/email/{emailId}
// Add a method that will return a specific email for a student.
// Be sure to provide a status code for any possible issues (i.e. student not found, email not found).
[HttpGet("student/{StudentId}/email/{EmailId}")]
public async Task<IActionResult> GetEmailByStudent([FromRoute] int StudentId, int EmailId)
{
var emailFound = await _context.Email.FirstOrDefaultAsync(e => e.EmailId == EmailId && e.StudentId == StudentId);
if (emailFound == null)
{
return NotFound("No combination of email/student ids found");
}
else
{
return Ok(emailFound);
}
}
// GET /api/1.0/student/{studentId}/emails
// Add a method that will return a list of email addresses (not student data) for a specific student.
[HttpGet("student/{id}/emails")]
public async Task<IActionResult> GetEmailsByStudent([FromRoute] int id)
{
List<Email> emailsFound = new List<Email>();
foreach (dynamic email in _context.Email)
{
if(email.StudentId == id)
{
emailsFound.Add(email);
}
}
if(emailsFound == null)
{
return NotFound("No Email Addresses found by Student Id");
}
return Ok(emailsFound);
}
// GET /api/1.0/student/findbyemail/{email}
// Add a method that will return a list of students that match an email address entered.
[HttpGet("student/findbyemail/{emailIn}")]
public async Task<IActionResult> GetStudentByEamilAddress([FromRoute] string emailIn)
{
// Create the list to be returned
List<Student> studentsFound = new List<Student>();
// Grab all emails where the EmailAddress matches the emailIn (Email passed in from Route)
var emailsFound = await _context.Email.Where(e => e.EmailAddress == emailIn).ToListAsync();
// Handle if there are no results
if(emailsFound.Count() == 0)
{
return NotFound("No Emails Found from: " + emailIn);
}
// Iterate through all results and grab the students (if there are students)
foreach(Email email in emailsFound)
{
Student student = await _context.Students.FirstOrDefaultAsync(s => s.StudentId == email.StudentId);
if (student != null)
{
studentsFound.Add(student);
}
}
// Handle for not student results
if(studentsFound.Count() == 0)
{
return NotFound("No Students Found by Email");
}
// Return Ok, and student data
return Ok(studentsFound);
}
// GET /api/1.0/student/findbydomain/{emailDomain}
// var studentEmails = await _context.Email.FirstOrDefaultAsync( e => e.EmailAddress.Contains("yahoo.com") );
[HttpGet("student/findbydomain/{emailDomain}/")]
public async Task<IActionResult> GetStudentsByEmailDomain([FromRoute] string emailDomain)
{
// Create the list to be returned
List<Student> studentsFound = new List<Student>();
// Grab all emails where the EmailAddress matches the emailIn (Email passed in from Route)
var emailsFound = await _context.Email.Where(e => e.EmailAddress.Contains(emailDomain)).ToListAsync();
// Handle if there are no results
if (emailsFound.Count() == 0)
{
return NotFound("No Emails Found from: " + emailDomain);
}
// Iterate through all results and grab the students (if there are students)
foreach (Email email in emailsFound)
{
Student student = await _context.Students.FirstOrDefaultAsync(s => s.StudentId == email.StudentId);
if (student != null)
{
studentsFound.Add(student);
}
}
// Handle for not student results
if (studentsFound.Count() == 0)
{
return NotFound("No Students Found by Email");
}
// Return Ok, and student data
return Ok(studentsFound);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment