Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CompanyApi/Company.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ public Company(string name)
{
Id = Guid.NewGuid().ToString();
Name = name;
Employees = new List<Employee>();
}

public string Id { get; set; }

public string Name { get; set; }
public List<Employee> Employees { get; set; }
}
}
97 changes: 97 additions & 0 deletions CompanyApi/Controllers/CompanyController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Copy link

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!🤩

{
return BadRequest();
}

var startIndex = (pageIndex - 1) * pageSize;
var pagedCompanies = companies.Skip(startIndex).Take(pageSize).ToList();

Choose a reason for hiding this comment

The 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();

Choose a reason for hiding this comment

The 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()
{
Expand Down
2 changes: 1 addition & 1 deletion CompanyApi/CreateCompanyRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
{
public class CreateCompanyRequest
{
public required string Name { get; set; }
public string Name { get; set; }
}
}
12 changes: 12 additions & 0 deletions CompanyApi/CreateEmployeeRequest.cs
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; }
}
}
14 changes: 14 additions & 0 deletions CompanyApi/Employee.cs
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; }
}
}
2 changes: 1 addition & 1 deletion CompanyApi/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://0.0.0.0:5116",
"applicationUrl": "http://localhost:5116",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
Expand Down
44 changes: 44 additions & 0 deletions CompanyApi/apis.http
Original file line number Diff line number Diff line change
Expand Up @@ -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

Choose a reason for hiding this comment

The 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}

Choose a reason for hiding this comment

The 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 )

######################################################################################
Loading