Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
… into 2017-2018
  • Loading branch information
a-omar committed Oct 9, 2018
2 parents d60d19f + 03dc852 commit 16bc87c
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 66 deletions.
2 changes: 1 addition & 1 deletion Lectures/Lecture2/practice.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@
"- In short try to query at most once the database per problem without nested toList() calls\n",
"\n",
"\n",
"### Logging\n",
"### Logging (extra material)\n",
"\n",
"- To understand the process of translating LINQ queries into SQL and to understand if the atomic behaviour of the queries is maintained, the EF provides a decorator that logs the queries to a file\n",
"\n",
Expand Down
Binary file modified Lectures/Lecture3/10.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Lectures/Lecture3/11.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Lectures/Lecture3/18.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Lectures/Lecture3/2.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Lectures/Lecture3/4b.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
109 changes: 53 additions & 56 deletions Lectures/Lecture3/practice.ipynb

Large diffs are not rendered by default.

23 changes: 14 additions & 9 deletions Lectures/Lecture3/theory.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
" - `PUT`\n",
" - `DELETE`\n",
" - A router, or routing rules, define which method is called by which URL, and what parts of the URL (if any) are parsed as parameters of the method\n",
"- All the API (methods) related to a single entity (for example `Student`) is usually put in a single class, but YMMV (_your mileage may vary_)\n",
"- All the API (methods) related to a single entity (for example `Student`) are usually put in a single class (in our example below, StudentApiController), but YMMV (_your mileage may vary_)\n",
"- What methods do we add?\n",
" - A very common approach to structure data access is called REST\n",
" - REST means _representational state transfer_\n",
Expand All @@ -49,47 +49,52 @@
" [HttpGet]\n",
" public IActionResult GetAll()\n",
" {\n",
" ...\n",
" return Ok(...);\n",
" }\n",
"\n",
" [HttpGet(\"{id}\")]\n",
" public IActionResult GetById(int id)\n",
" {\n",
" ...\n",
" return Ok(...);\n",
" } \n",
"\n",
" [HttpPost]\n",
" public IActionResult Create([FromBody] Student student)\n",
" {\n",
" ...\n",
" return Ok(...);\n",
" }\n",
"\n",
" [HttpPut]\n",
" public IActionResult Update([FromBody] Student modified_student)\n",
" {\n",
" ...\n",
" return Ok(...);\n",
" }\n",
"\n",
" [HttpDelete(\"{id}\")]\n",
" public IActionResult Delete(int id)\n",
" {\n",
" ...\n",
" return Ok(...);\n",
" }\n",
"}\n",
"\n",
"```\n",
"\n",
"- The class above implements a RESTful interface on `xxx/students`\n",
" - note that the router is just an attribute above the class\n",
" - plus the argument to the attribute above each method\n",
" - plus the null configuration `app.UseMvc(routes => {});` in `startup.cs`\n",
" - An API call automatically transfers data via JSON, which is serialized and deserialized automatically (structural errors prevent completion of the call for type safety and security reasons)\n",
"- The class above implements a RESTful interface on `xxx/students`. In general, in .NET an application is RESTful if:\n",
" - there is a class implementing the router attribute (`[Route(\"...\")]`)\n",
" - there is a series of methods implementing get, post, put and delete operations\n",
" - `startup.cs` contains `app.UseMvc(routes => {});` (null configuration), necessary to register the router to the services exposed by the server\n",
"- An API call automatically transfers data via JSON, which is serialized and deserialized automatically (structural errors prevent completion of the call for type safety and security reasons)\n",
"- The body of the methods is just a combination of LINQ and regular code\n",
" - `Ok` denotes that we give a result back (the argument is the result, seralized to JSON)\n",
" - There are various methods such as `Ok` that we can give to signal errors, such as _not found_, _unauthorized_, and much more\n",
"- Suppose now that we have a lot of students (hundreds of thousands)\n",
" - `GetAll` is a bit of an issue\n",
" - A malicious attacker could cause a DDOS by simply spinning up a lot of requests\n",
" - A malicious attacker could cause a DDOS (distributed denial of service) by simply spinning up a lot of requests\n",
" - Requests would all eventually fail because of timeout\n",
" - Our server would be quickly overwhelmed\n",
"- The solution is pagination: we return a block of data (called _a page_), never bigger than a given amount of elements\n",
Expand All @@ -99,7 +104,7 @@
"public IActionResult GetAll([FromQuery] int page_index, [FromQuery] int page_size)\n",
"```\n",
"\n",
"- `GetAll` will now return the elements from `page_index * page_size` to `page_index * page_size + page_size`\n",
"- `GetAll` will now return the elements from `page_index * page_size` to `page_index * page_size + page_size - 1`\n",
"- We can impose a maximum cap for `page_size`, so that if the user asks for more than, say, 100 students, we still only give 100 back\n",
"- `GetAll` will therefore return a JSON result looking like\n",
"\n",
Expand All @@ -113,8 +118,8 @@
"```\n",
"\n",
"- notice that the parameters `page_index` and `page_size` come from the query part of the URL itself\n",
" - For example: `xxx/students?page_index=10&page_size=25`\n",
" - Everything after the `?` is the query\n",
" - `xxx/students?page_index=10&page_size=25`\n",
"\n",
"- `GetAll` now contains basic filtering (basic because it is simply a range)\n",
"- it is possible to add more parameters to perform extra filtering\n",
Expand Down

0 comments on commit 16bc87c

Please sign in to comment.