-
Notifications
You must be signed in to change notification settings - Fork 4
Shuhan Jin #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Shuhan Jin #5
Changes from all commits
44e65ce
909b3a5
e8ef723
5939096
c8edae5
048ff12
11c2a2c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using System.Linq; | ||
|
||
namespace CompanyApi.Controllers | ||
{ | ||
|
@@ -22,8 +23,83 @@ public ActionResult<Company> Create(CreateCompanyRequest request) | |
|
||
[HttpDelete] | ||
public void ClearData() | ||
{ | ||
{ | ||
companies.Clear(); | ||
} | ||
|
||
[HttpGet] | ||
public ActionResult<List<Company>> GetAll([FromQuery] int? pageSize, int? pageIndex) | ||
{ | ||
if (pageSize != null && pageIndex != null) | ||
{ | ||
var startIndex = (pageIndex - 1) * pageSize; | ||
var companiesPage = companies.Skip((int)startIndex).Take((int)pageSize).ToList(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WangKe: |
||
return Ok(companiesPage); | ||
} | ||
|
||
return companies; | ||
} | ||
|
||
[HttpGet("{id}")] | ||
public ActionResult<Company> Get(string id) | ||
{ | ||
var company = companies.Find(cp => cp.Id == id); | ||
|
||
if (company != null) | ||
{ | ||
return company; | ||
} | ||
|
||
return NotFound(); | ||
} | ||
|
||
[HttpPut("{id}")] | ||
public ActionResult<Company> Update(string id, [FromBody] Company updatedCompany) | ||
{ | ||
var existingCompany = companies.Find(c => c.Id == id); | ||
if (existingCompany == null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
existingCompany.Name = updatedCompany.Name; | ||
|
||
return NoContent(); | ||
} | ||
|
||
[HttpPost("{companyId}/employees")] | ||
public ActionResult<Company> AddEmployee(string companyId, [FromBody] Employee employee) | ||
{ | ||
var company = companies.Find(c => c.Id == companyId); | ||
if (company == null) | ||
{ | ||
return NotFound(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. well: use 404 if company can not be found👍 |
||
} | ||
|
||
//employee.Id = Guid.NewGuid().ToString(); | ||
company.Employees.Add(employee); | ||
|
||
return Ok(employee); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As we add new employee here, maybe [return Created(employee)] could be better |
||
} | ||
|
||
[HttpDelete("{companyId}/employees/{employeeId}")] | ||
public ActionResult<Company> DeleteEmployee(string companyId, string employeeId) | ||
{ | ||
var company = companies.Find(c => c.Id == companyId); | ||
if (company == null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
var employee = company.Employees.Find(e => e.Id == employeeId); | ||
if (employee == null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
company.Employees.Remove(employee); | ||
|
||
return NoContent(); //bc delete doesnt return anything | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System.Xml.Linq; | ||
|
||
namespace CompanyApi | ||
{ | ||
public class Employee | ||
{ | ||
public Employee(string name) | ||
{ | ||
Id = Guid.NewGuid().ToString(); | ||
Name = name; | ||
} | ||
|
||
public string Id { get; set; } | ||
public string Name { get; set; } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,4 +58,41 @@ Content-Type: application/json; charset=utf-8 | |
### Case1: Sucess, 204 NO Content | ||
### Case2: 404 Company NOT Found | ||
|
||
###################################################################################### | ||
###################################################################################### | ||
|
||
## AC6 add employee to a specific company | ||
PUT https://{{hostname}}:{{port}}/api/Companies/{companyID}/employee | ||
Content-Type: application/json; charset=utf-8 | ||
|
||
Company: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: not need this json for company, we find related company by companyID in the URL |
||
{ | ||
"name": Google | ||
} | ||
|
||
employee: | ||
{ | ||
"name": Eleanor | ||
} | ||
### Response: | ||
### Case1: Sucess, 200 ok, with the employee name | ||
### Case2: 404 Company NOT Found | ||
|
||
###################################################################################### | ||
|
||
## AC7 delete a specific employee from a specific company | ||
PUT https://{{hostname}}:{{port}}/api/Companies/{companyID}/employee/{employeeID} | ||
Content-Type: application/json; charset=utf-8 | ||
|
||
Company: | ||
{ | ||
"name": Google | ||
} | ||
|
||
employee: | ||
{ | ||
"name": Eleanor | ||
} | ||
### Response: | ||
### Case1: success, 204 no content | ||
### Case2: 404 Company NOT Found | ||
### Case3: 404 emloyee NOT Found | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to be improved: we also need to design api for AC 8-10 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
well: nice to use Skip and Take