-
-
Save jipengxiang/32dda180721ba5f624260ec35633668b to your computer and use it in GitHub Desktop.
(1) PLease upload your client side code to call talents API | |
(2) AWS url for web API if you have PUBLISHED your web api to AWS | |
or url to S3 image | |
(3) Sequence diagram for calling Stripe API | |
or calling global weather web service |
1503393 | Tang Guan You Darryl
1. TalentsController.cs
using ProductStore.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace ProductStore.Controllers
{
public class TalentsController : ApiController
{
static readonly TalentRepository repository = new TalentRepository();
[Route("api/talents")]
public IEnumerable<Talent> GetAllTalents()
{
return repository.GetAll();
}
[Route("api/talents/{id:int}")]
public Talent GetTalent(int id)
{
Talent item = repository.Get(id);
if (item == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return item;
}
}
}
3.
Some Screenshots for API annotations [Task 3]
Trying to POST object that is not compliant with annotated fields.
Attempting to POST object with fields that are not within pre-definied model.
Some screenshots regarding HTTPS authentication [Task 5]
When requesting with http://, it gets redirected to HTTPS. Below shows that an SSL protocol is needed to go through
Using Postman, there is no response when using http
Database Driven Approach
public class TalentController : ApiController
{
CSC_DatabaseEntitiesv1 db = new CSC_DatabaseEntitiesv1();
///
/// Get all talents objects from database
///
///
[RequireHttps]
[HttpGet]
[EnableCors(origins: "", headers: "", methods: "*")]
[Route("api/talents")]
public IEnumerable GetAllTalents()
{
List talents = db.Talents.ToList();
System.Threading.Thread.Sleep(1500);
return talents;
}
/// <summary>
/// Get one talent object from database by id
/// </summary>
/// <param name="id">The id of the talent object</param>
/// <returns></returns>
[Route("api/talents/{id:int}")]
public Talent GetTalent(int id)
{
Talent item = db.Talents.SingleOrDefault(x => x.Id == id);
if (item == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return item;
}
}
Model
Adding a Database Model
Reference
https://www.youtube.com/watch?v=UrHKfCCncHQ (Start from 2:03)
- AJAX Timeout Retry
$.ajax({ type: 'GET', url: url, async: false, contentType: "application/json", jsonpCallback: callback, dataType: 'jsonp', tryCount: 0, retryLimit: 3, success: function (json) { console.dir('success'); }, error: function (xhr, textStatus, errorThrown ) { if (textStatus == 'timeout') { this.tryCount++; if (this.tryCount <= this.retryLimit) { sleep(1000); $.ajax(this); return; } return; } console.log(e.message); } });
- Entity Framework (DotNet Core)
2.1 Startup - Specify services used
public void ConfigureServices(IServiceCollection services) { services.AddDbContext<TalentDbContext>( options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); services.AddScoped<ITalentRepository, SqlTalentRepository>(); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); }
2.2 TalentDbContext - Create DB Context
`
public class TalentDbContext : DbContext
{
public TalentDbContext(DbContextOptions options) : base(options) {}
public DbSet<Talent> Talents { get; set; }
}
`
2.3 SqlTalentRepository - DB CRUD operations
`
public class SqlTalentRepository : ITalentRepository
{
private TalentDbContext _context;
public SqlTalentRepository(TalentDbContext context)
{
_context = context;
}
public void Post(Talent inputTalent)
{
_context.Add(inputTalent);
_context.SaveChanges();
}
public IEnumerable<Talent> GetAll() => _context.Talents;
public Talent Get(int inputId) => _context.Talents.FirstOrDefault(t => t.Id == inputId);
public void Put(int inputId, Talent inputTalent)
{
Talent talent = _context.Talents.FirstOrDefault(x => x.Id == inputId);
if (talent != null)
{
_context.Talents.Remove(talent);
_context.Talents.Add(inputTalent);
}
_context.SaveChanges();
}
public void Delete(int inputId)
{
Talent talent = _context.Talents.FirstOrDefault(x => x.Id == inputId);
_context.Talents.Remove(talent);
_context.SaveChanges();
}
}
`
3. AJAX Loading
$(document).ajaxStart(function () { $("#loading").show(); }).ajaxStop(function () { $("#loading").hide(); });
Lim Wen Hui 1646348
public class TalentsController : ApiController
{
static readonly TalentRepository repository = new TalentRepository();
[EnableCors(origins: "", headers: "", methods: "*")]
// download Microsoft.AspNet.Cors package library
[Route("api/talents")]
public IEnumerable GetAllTalents()
{
return repository.GetAll();
}
[Route("api/talents/{id:int}")]
public Talent GetTalent(int id)
{
Talent item = repository.Get(id);
if (item == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return item;
}
}
1626373, Koh Ding Yuan
Talents API Controller
` [ApiController, Route("api/talents"), RequireHttps]
public class TalentsController : ControllerBase
{
private ITalentRepository talentRepository = new TalentRepository();
#region TalentsController - Crud Methods
//Create Method
[HttpPost]
public Task PostTalent([BindRequired]TalentView inputTalent)
{
try
{
#region PostTalent - Model Validation
if (!ModelState.IsValid) {
return Task.FromResult(
BadRequest("Invalid object as parameter."));
}
#endregion
#region PostTalent - Conflict Validation
Talent tempTalent = talentRepository.Get(inputTalent.Id);
if (tempTalent != null) {
return Task.FromResult(
Conflict("Talent with the id of " + inputTalent.Id + " already exists."));
}
#endregion
#region PostTalent - Data Processing
talentRepository.Post(inputTalent);
return Task.FromResult(
Ok("Talent with the id of " + inputTalent.Id + " was successfully created."));
#endregion
}
catch (Exception) {
return Task.FromResult(
StatusCode(500));
}
}
}`
AWS S3 Url
https://s3-us-west-2.amazonaws.com/ql-cf-templates-1541035926-39999b7229d833b5-us-west-2/
Sequence Diagram
![sequence_diagram](https://user-images.githubusercontent.com/24997326/51363362-95e9ce80-1b12-11e9-8449-7d46073a14d5.png)