Specifications Create a single-page application with an HTML form and database.
ID: 3792398 • Letter: S
Question
Specifications
Create a single-page application with an HTML form and database.
The form will include the following:
Input text field for Zip Code
Submit button
The zip code text field must retain its value after form is submitted.
If an invalid zip code is entered, display an error message.
An invalid zip code is a string that is not made up of 5 digits.
Hint: Use the is_numeric and strlen functions.
If a valid zip code is entered, but not found in the database, display an appropriate message.
If a valid zip code is entered and is found in the database but no people are found within 25 miles, display an appropriate message.
When a valid zip code is entered and people are found within 25 miles, display a report comprised of the following:
Report heading:
Display information about the location selected:
zip code
city
state
the total number of association members within 25 miles
Report Matches:
A table displaying information about the nearby people, with these columns:
Person
Provider number
City
State
Zip
The distance from the selected location. (in miles)
The table will be sorted by distance, closest on top.
Secondary sorting is by provider number
Implementation Details
Use the a6_locations and a6_people tables.
Find the one record in the a6_locations table that matches the entered zip code.
Use this information for your report heading, and to set the $lat and $log variables mentioned below.
Join the two tables together using locationID
To find locations that are within 25 miles, use this formula:
I have two variables, $lat and $log, representing the latitude and longitude from the record found in step 2.
My WHERE clause would be:
WHERE 69*(sqrt(pow($lat-latitude,2) + pow($log-longitude,2))) < 25
sqrt is the MySQL function for square root, and pow is the MySQL function for "to the power of".
Explanation / Answer
1):
Creating the Service Layer:
First, I created a Movie class that represents a movie. This class does two things:
Tells Entity Framework (EF) how to create the database tables to store the movie data.
Tells Web API how to format the JSON payload.
namespace MoviesSPA.Models
{
public class Movie
{
public int ID { get; set; }
public string Title { get; set; }
public int Year { get; set; }
public string Genre { get; set; }
public string Rating { get; set; }
}
}
step2:
I extended this API by adding a method that finds all the movies in a specified genre:
public class MoviesController : ApiController
{
public IQueryable<Movie> GetMoviesByGenre(string genre)
{
return db.Movies.Where(m =>
m.Genre.Equals(genre, StringComparison.OrdinalIgnoreCase));
}
step3
UI triggers an AJAX request
Update the HTML to display the response payload
Handle AJAX errors
. For example, here’s some jQuery code that creates a list of movie titles:
$.getJSON(url)
.done(function (data) {
// On success, "data" contains a list of movies
var ul = $("<ul></ul>")
$.each(data, function (key, item) {
// Add a list item
$('<li>', { text: item.Title }).appendTo(ul);
});
$('#movies').html(ul);
});
step4:
<!DOCTYPE html>
<html>
<head>
<title>Movies SPA</title>
</head>
<body>
<ul>
<li><a href="#"><!-- Genre --></a></li>
</ul>
<table>
<thead>
<tr><th>Title</th><th>Year</th><th>Rating</th>
</tr>
</thead>
<tbody>
<tr>
<td><!-- Title --></td>
<td><!-- Year --></td>
<td><!-- Rating --></td></tr>
</tbody>
</table>
<p><!-- Error message --></p>
<p>No records found.</p>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.