-
Notifications
You must be signed in to change notification settings - Fork 4
Kailin company api #2
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?
Changes from all commits
1d03ac5
ab99dad
7bef7c7
831ba60
a6f2895
f2db82c
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 |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ namespace CompanyApi.Controllers | |
| public class CompanyController : ControllerBase | ||
| { | ||
| private static List<Company> companies = new List<Company>(); | ||
| private static Dictionary<string, List<Employee>> employees = new Dictionary<string, List<Employee>>(); | ||
|
|
||
| [HttpPost] | ||
| public ActionResult<Company> Create(CreateCompanyRequest request) | ||
|
|
@@ -20,6 +21,102 @@ public ActionResult<Company> Create(CreateCompanyRequest request) | |
| return StatusCode(StatusCodes.Status201Created, companyCreated); | ||
| } | ||
|
|
||
| [HttpGet("all")] | ||
| public ActionResult<List<Company>> GetAll() | ||
| { | ||
| return companies; | ||
| } | ||
|
|
||
| [HttpGet] | ||
| public ActionResult<List<Company>> GetAllByIndex([FromQuery] int pageSize = 0, [FromQuery] int pageIndex = 1) | ||
| { | ||
| if (pageSize <= 0 || pageIndex <= 0) | ||
| { | ||
| return BadRequest(); | ||
| } | ||
|
|
||
| var startIndex = (pageIndex - 1) * pageSize; | ||
| var pagedCompanies = companies.Skip(startIndex).Take(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. 👍 nice to use Skip Take |
||
|
|
||
| return Ok(pagedCompanies); | ||
| //return companies; | ||
| } | ||
|
|
||
| [HttpGet("{id}")] | ||
| public ActionResult<Company> GetById(string id) | ||
| { | ||
| var company = companies.Find(company => company.Id == id); | ||
| if (company == null) | ||
| { | ||
| return NotFound(); | ||
| } | ||
| return Ok(company); | ||
| } | ||
|
|
||
| [HttpPut("{id}")] | ||
| public ActionResult<Company> UpdateCompany(string id, [FromBody] Company updatedCompany) | ||
| { | ||
| var company = companies.Find(c => c.Id == id); | ||
|
|
||
| if (company == null) | ||
| { | ||
| return NotFound(); | ||
| } | ||
|
|
||
| company.Name = updatedCompany.Name; | ||
| return Ok(company); | ||
| } | ||
|
|
||
| [HttpPost("{companyId}/employees")] | ||
| public ActionResult<Company> Create([FromRoute] string companyId, CreateEmployeeRequest request) | ||
| { | ||
| var company = companies.Find(c => c.Id == companyId); | ||
| if (company is null) | ||
| { | ||
| return BadRequest(); | ||
|
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: return 404 if the company can not be found |
||
| } | ||
|
|
||
| if (!employees.ContainsKey(companyId)) | ||
| { | ||
| employees[companyId] = new List<Employee>(); | ||
| } | ||
|
|
||
| if (employees[companyId].Find(employee => employee.Name == request.Name) is not null) | ||
| { | ||
| return BadRequest(); | ||
| } | ||
|
|
||
| Employee employeeCreated = new Employee(request.Name); | ||
| employees[companyId].Add(employeeCreated); | ||
| return StatusCode(StatusCodes.Status201Created, employeeCreated); | ||
| } | ||
|
|
||
| [HttpDelete("{companyId}/employees/{employeeId}")] | ||
| public ActionResult<Company> DeleteEmployee([FromRoute] string companyId, [FromRoute] string employeeId) | ||
| { | ||
| var company = companies.Find(c => c.Id == companyId); | ||
| if (company is null) | ||
| { | ||
| return NotFound(); | ||
| } | ||
|
|
||
| if (!employees.ContainsKey(companyId)) | ||
| { | ||
| return NotFound(); | ||
| } | ||
|
|
||
| Employee employee = employees[companyId].Find(employee => employee.Id == employeeId); | ||
| if (employee is null) | ||
| { | ||
| return NotFound(); | ||
| } | ||
| else | ||
| { | ||
| employees[companyId].Remove(employee); | ||
| return StatusCode(StatusCodes.Status204NoContent, employee); | ||
| } | ||
| } | ||
|
|
||
| [HttpDelete] | ||
| public void ClearData() | ||
| { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| namespace CompanyApi | ||
| { | ||
| public class CreateEmployeeRequest | ||
| { | ||
| public CreateEmployeeRequest(string name) | ||
| { | ||
| Name = name; | ||
| } | ||
|
|
||
| public string Name { get; set; } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| 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,48 @@ Content-Type: application/json; charset=utf-8 | |
| ### Case1: Sucess, 204 NO Content | ||
| ### Case2: 404 Company NOT Found | ||
|
|
||
| # Employees APIS | ||
| ## POST /api/companies/companyID/employees | ||
| ## GET /api/companies/companyID/employees | ||
| ## PUT /api/companies/companyID/employees/{employeeId} | ||
| ## DELETE /api/companies/companyID/employees/{employeeId} | ||
|
|
||
| ## AC6 Create Employee API | ||
| PUT https://{{hostname}}:{{port}}/api/companies/{companyId}/employees | ||
|
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: use POST instead of PUT to create resource |
||
| Content-Type: application/json; charset=utf-8 | ||
|
|
||
| { | ||
| "name": "xianke" | ||
| } | ||
| ## Response: | ||
| ### Case1: Employee Created Success, Return Employee created with Status Created( 201 ) | ||
| ### Case2: Employee Name Exsiting, Return 400 Bad request | ||
| ### Case3: Other Bad Reqeust ( Request Body Not Valid ) | ||
|
|
||
| ## AC7 Delete Employee by ID | ||
| DELETE https://{{hostname}}:{{port}}/api/companies/{companyId}/employees/{employeeId} | ||
|
|
||
| ### Response: | ||
| ### Case1: Employee Deleted Success, Return Employee with Status NO CONTENT( 204 ) | ||
| ### Case2: Employee Not Found, Return Status NOT FOUND( 404 ) | ||
|
|
||
| ## AC8 Get All Employees in company with companyId | ||
| GET https://{{hostname}}:{{port}}/api/companies/{companyId}/employees | ||
|
|
||
| ### Response: Return Employee list with Status 200 | ||
|
|
||
| ## AC9 Update Employee | ||
| PUT https://{{hostname}}:{{port}}/api/companies/{companyId}/employees/{employeeId} | ||
|
|
||
|
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: add request body for PUT |
||
| ### Response: | ||
| ### Case1: Employee Update Sucess, Return with Status 200 | ||
| ### Case2: 404 Employee NOT Found | ||
|
|
||
| ## AC10 Delete Company by ID | ||
| DELETE https://{{hostname}}:{{port}}/api/companies/{companyId} | ||
|
|
||
| ### Response: | ||
| ### Case1: Company Deleted Success, Return OK with Status NO CONTENT( 204 ) | ||
| ### Case2: Company Not Found, Return Status NOT FOUND( 404 ) | ||
|
|
||
| ###################################################################################### | ||
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.
Good: Give a init pageSize and pageIndex value. Stick with the rules!🤩