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
6 changes: 4 additions & 2 deletions CompanyApi.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.7.34202.233
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CompanyApi", "CompanyApi\CompanyApi.csproj", "{AB9810FE-4FFB-4890-B2F0-FC002BFE376E}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CompanyApi", "CompanyApi\CompanyApi.csproj", "{AB9810FE-4FFB-4890-B2F0-FC002BFE376E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CompanyApiTest", "CompanyApiTest\CompanyApiTest.csproj", "{CB3506BC-1633-4CB6-A561-B39DDCF59168}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CompanyApiTest", "CompanyApiTest\CompanyApiTest.csproj", "{CB3506BC-1633-4CB6-A561-B39DDCF59168}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{F21EEDA1-5E42-4F81-9909-E2E2555C5046}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
3 changes: 3 additions & 0 deletions CompanyApi/Company.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ 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; }
}
}
68 changes: 67 additions & 1 deletion CompanyApi/Controllers/CompanyController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc;
using System.Linq;

namespace CompanyApi.Controllers
{
Expand All @@ -22,8 +23,73 @@ public ActionResult<Company> Create(CreateCompanyRequest request)

[HttpDelete]
public void ClearData()
{
{
companies.Clear();
}

[HttpGet("{id}")]
public ActionResult<Company> Get(string id)
{
foreach (var company in companies.Where(company => company.Id.Equals(id)))
{
return Ok(company);
}

return NotFound();
}

[HttpGet]
public ActionResult<List<Company>> GetInPage([FromQuery] int? pageSize, [FromQuery] int? pageIndex)
{
if (pageIndex == null||pageSize == null)
{
return Ok(companies);
}
List<Company> companiesInPage = companies.Skip(((int)pageIndex -1)* (int)pageSize).Take((int)pageSize).ToList();

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 Take
suggestion: extract (pageIndex - 1) * pageSize to a temp variable

return Ok(companiesInPage);
}


[HttpPut("{id}")]
public ActionResult<Company> Update(string id, CreateCompanyRequest companyRequest)
{
foreach (var company in companies.Where(company => company.Id.Equals(id)))
{
company.Name = companyRequest.Name;
return NoContent();
}
return NotFound();
}

[HttpPost("{id}/employees")]
public ActionResult<Employee> AddEmployee(string id, CreateEmployeeRequest request)
{
Employee employeeCreated = new Employee(request.Name);
foreach (var company in companies.Where(company => company.Id.Equals(id)))
{
if (company.Employees.Exists(employee => employee.Name.Equals(request.Name)))
{
return BadRequest();
}
company.Employees.Add(employeeCreated);
return StatusCode(StatusCodes.Status201Created, employeeCreated);
}
return NotFound();
}

[HttpDelete("{companyId}/employees/{employeeId}")]
public ActionResult DeleteEmployeeFromSpecificCompany(string companyId, string employeeId)
{

foreach (var (company, employee) in from company in companies
where company.Id.Equals(companyId)
from employee in company.Employees.Where(employee => employee.Id.Equals(employeeId))
select (company, employee))
{
company.Employees.Remove(employee);
return NoContent() ;
}
return NotFound() ;
}
}
}
7 changes: 7 additions & 0 deletions CompanyApi/CreateEmployeeRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace CompanyApi
{
public class CreateEmployeeRequest
{
public required string Name { get; set; }
}
}
16 changes: 16 additions & 0 deletions CompanyApi/Employee.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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; }
}
}

49 changes: 49 additions & 0 deletions CompanyApi/apis.http
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,53 @@ Content-Type: application/json; charset=utf-8
### Case1: Sucess, 204 NO Content
### Case2: 404 Company NOT Found

######################################################################################
## AC6 Add an employee to a specific company
POST https://{{hostname}}:{{port}}/api/Companies/{companyID}
Content-Type: application/json; charset=utf-8

{
"Name": Tom
}
### Response:
### Case1: Created Success, Return employee created with Status Created( 201 )
### Case2: Employee Name Exsiting, Return 400 Bad request
### Case3: 404 company not found
### Case4: Bad Request (invalid request body)

######################################################################################
## AC7 Delete an employee from a specific company
DELETE https://{{hostname}}:{{port}}/api/Companies/{companyID}/employees/{employeeID}

### Response:
### Case1: Sucess, 204 NO Content
### Case2: 404 NOT Found(Wrong Employee id)
### Case3: 404 NOT Found(Wrong Company id)

######################################################################################
## AC8 Get all employees from a specific company
GET https://{{hostname}}:{{port}}/api/Companies/{companyID}/employees

### Response: OK 200 with List of Employees

######################################################################################
## AC9 Update the information of a specific employee from a certain company
PUT https://{{hostname}}:{{port}}/api/Companies/{companyID}/employees/{employeeID}
Content-Type: application/json; charset=utf-8

{
"name": Jim
}
### Response:
### Case1: Sucess, 204 NO Content
### Case2: 404 Company NOT Found

######################################################################################
## AC10 Delete a specific company and remove all the employee.
DELETE https://{{hostname}}:{{port}}/api/Companies/{companyID}

### Response:
### Case1: Sucess, 204 NO Content
### Case2: 404 Company NOT Found

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