Skip to content

Commit

Permalink
Update to P14
Browse files Browse the repository at this point in the history
  • Loading branch information
Surbowl committed Jan 11, 2020
1 parent 139a2cd commit 99f1c49
Show file tree
Hide file tree
Showing 13 changed files with 558 additions and 34 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ __ASP.NET Core 3.0 实现 RESTful API 的学习笔记__
<br><br>
跟随杨旭老师(solenovex)的[博客](https://www.cnblogs.com/cgzl/p/11814971.html)[视频](https://www.bilibili.com/video/av77957694?from=search&seid=17664776753878261104)课程,学习 RESTful API 在ASP.NET Core 3.0 上的实现。
<br>
包含课程中搭建的项目与部分笔记,当前更新到`视频P8`
包含课程中搭建的项目与部分笔记,此项目的内容更新到视频 P14
<br><br><br>
非常感谢杨老师 🤗
<br>
42 changes: 25 additions & 17 deletions Routine/Routine.APi/Controllers/CompaniesController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.AspNetCore.Mvc;
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Routine.APi.Models;
using Routine.APi.Services;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -55,40 +57,46 @@ namespace Routine.APi.Controllers
public class CompaniesController:ControllerBase
{
private readonly ICompanyRepository _companyRepository;
private readonly IMapper _mapper;

public CompaniesController(ICompanyRepository companyRepository)
public CompaniesController(ICompanyRepository companyRepository,IMapper mapper)
{
this._companyRepository = companyRepository ??
_companyRepository = companyRepository ??
throw new ArgumentNullException(nameof(companyRepository));
_mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
}

[HttpGet]
public async Task<IActionResult> GetCompanies()
public async Task<IActionResult> GetCompanies() //Task<IActionResult> = Task<ActionResult<List<CompanyDto>>>
{
var companies = await _companyRepository.GetCompaniesAsync();
return Ok(companies); //OK() 返回状态码200

//不使用 AutoMapper
//var companyDtos = new List<CompanyDto>();
//foreach(var company in companies)
//{
// companyDtos.Add(new CompanyDto
// {
// Id = company.Id,
// Name = company.Name
// });
//}

//使用 AutoMapper
var companyDtos = _mapper.Map<IEnumerable<CompanyDto>>(companies);

return Ok(companyDtos); //OK() 返回状态码200
}

[HttpGet("{companyId}")] //还可用 [Route("{companyId}")]
public async Task<IActionResult> GetCompany(Guid companyId)
{
//不适合高并发的方法:
//var exist = await _companyRepository.CompanyExistsAsync(companyId);
//if (!exist)
//{
// return NotFound(); //返回状态码404
//}
//var company = await _companyRepository.GetCompanyAsync(companyId);
//return Ok(company);
//

//略有改善的方法:
var company = await _companyRepository.GetCompanyAsync(companyId);
if (company == null)
{
return NotFound(); //返回状态码404
}
return Ok(company);
return Ok(_mapper.Map<CompanyDto>(company));
}

}
Expand Down
61 changes: 61 additions & 0 deletions Routine/Routine.APi/Controllers/EmployeesController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Routine.APi.Entities;
using Routine.APi.Models;
using Routine.APi.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Routine.APi.Controllers
{
[ApiController]
[Route("api/companies/{companyId}/employees")]
public class EmployeesController : ControllerBase
{
private readonly ICompanyRepository _companyRepository;
private readonly IMapper _mapper;

public EmployeesController(ICompanyRepository companyRepository, IMapper mapper)
{
_companyRepository = companyRepository ?? throw new ArgumentNullException(nameof(companyRepository));
_mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));

}

[HttpGet]
public async Task<IActionResult> GetEmployeesForCompany(Guid companyId)
{
if (await _companyRepository.CompanyExistsAsync(companyId))
{
var employees = await _companyRepository.GetEmployeesAsync(companyId);
var employeeDtos = _mapper.Map<IEnumerable<EmployeeDto>>(employees);
return Ok(employeeDtos);
}
else
{
return NotFound();
}
}

[HttpGet("{employeeId}")]
public async Task<IActionResult> GetEmployeesForCompany(Guid companyId,Guid employeeId)
{
if (await _companyRepository.CompanyExistsAsync(companyId))
{
var employee = await _companyRepository.GetEmployeeAsync(companyId,employeeId);
if (employee == null)
{
return NotFound();
}
var employeeDto = _mapper.Map<EmployeeDto>(employee);
return Ok(employeeDto);
}
else
{
return NotFound();
}
}
}
}
95 changes: 79 additions & 16 deletions Routine/Routine.APi/Data/RoutineDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Routine.APi.Data
public class RoutineDbContext : DbContext
{
//调用并获取父类的options
public RoutineDbContext(DbContextOptions<RoutineDbContext>options):base(options)
public RoutineDbContext(DbContextOptions<RoutineDbContext> options) : base(options)
{

}
Expand All @@ -21,10 +21,10 @@ public RoutineDbContext(DbContextOptions<RoutineDbContext>options):base(options)
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Company>().Property(x => x.Name).IsRequired().HasMaxLength(100);
modelBuilder.Entity<Company>().Property(x=>x.Introduction).HasMaxLength(500);
modelBuilder.Entity<Employee>().Property(x=>x.EmployeeNo).IsRequired().HasMaxLength(10);
modelBuilder.Entity<Employee>().Property(x=>x.FirstName).IsRequired().HasMaxLength(50);
modelBuilder.Entity<Employee>().Property(x=>x.LastName).IsRequired().HasMaxLength(50);
modelBuilder.Entity<Company>().Property(x => x.Introduction).HasMaxLength(500);
modelBuilder.Entity<Employee>().Property(x => x.EmployeeNo).IsRequired().HasMaxLength(10);
modelBuilder.Entity<Employee>().Property(x => x.FirstName).IsRequired().HasMaxLength(50);
modelBuilder.Entity<Employee>().Property(x => x.LastName).IsRequired().HasMaxLength(50);
modelBuilder.Entity<Employee>()
//指明一对多关系(可省略)
.HasOne(x => x.Company)
Expand All @@ -33,26 +33,89 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
.HasForeignKey(x => x.CompanyId)
//删除Company时如果有Employee,则无法删除
.OnDelete(DeleteBehavior.Restrict);
//种子数据
modelBuilder.Entity<Company>().HasData(
new Company
new Company
{
Id=Guid.Parse("bbdee09c-089b-4d30-bece-44df5923716c"),
Name="Microsoft",
Introduction="Great Company"
Id = Guid.Parse("bbdee09c-089b-4d30-bece-44df5923716c"),
Name = "Microsoft",
Introduction = "Great Company"
},
new Company
{
Id=Guid.Parse("6fb600c1-9011-4fd7-9234-881379716440"),
Name="Google",
Introduction="Don't be evil"
Id = Guid.Parse("6fb600c1-9011-4fd7-9234-881379716440"),
Name = "Google",
Introduction = "Don't be evil"
},
new Company
{
Id=Guid.Parse("5efc910b-2f45-43df-afee-620d40542853"),
Name="Alipapa",
Introduction="Fubao Company"
Id = Guid.Parse("5efc910b-2f45-43df-afee-620d40542853"),
Name = "Alipapa",
Introduction = "Fubao Company"
}
);
modelBuilder.Entity<Employee>().HasData(
new Employee
{
Id=Guid.Parse("ca268a19-0f39-4d8b-b8d6-5bace54f8027"),
CompanyId = Guid.Parse("bbdee09c-089b-4d30-bece-44df5923716c"),
DateOfBirth = new DateTime(1955, 10, 28),
EmployeeNo = "M001",
FirstName = "William",
LastName = "Gates",
Gender = Gender.
},
new Employee
{
Id = Guid.Parse("265348d2-1276-4ada-ae33-4c1b8348edce"),
CompanyId = Guid.Parse("bbdee09c-089b-4d30-bece-44df5923716c"),
DateOfBirth = new DateTime(1998, 1, 14),
EmployeeNo = "M024",
FirstName = "Kent",
LastName = "Back",
Gender = Gender.
},
new Employee
{
Id = Guid.Parse("47b70abc-98b8-4fdc-b9fa-5dd6716f6e6b"),
CompanyId = Guid.Parse("6fb600c1-9011-4fd7-9234-881379716440"),
DateOfBirth = new DateTime(1986, 11, 4),
EmployeeNo = "G003",
FirstName = "Mary",
LastName = "King",
Gender = Gender.
},
new Employee
{
Id = Guid.Parse("059e2fcb-e5a4-4188-9b46-06184bcb111b"),
CompanyId = Guid.Parse("6fb600c1-9011-4fd7-9234-881379716440"),
DateOfBirth = new DateTime(1977, 4, 6),
EmployeeNo = "G007",
FirstName = "Kevin",
LastName = "Richardson",
Gender = Gender.
},
new Employee
{
Id = Guid.Parse("a868ff18-3398-4598-b420-4878974a517a"),
CompanyId = Guid.Parse("5efc910b-2f45-43df-afee-620d40542853"),
DateOfBirth = new DateTime(1964, 9, 10),
EmployeeNo = "A001",
FirstName = "Jack",
LastName = "Ma",
Gender = Gender.
},
new Employee
{
Id = Guid.Parse("2c3bb40c-5907-4eb7-bb2c-7d62edb430c9"),
CompanyId = Guid.Parse("5efc910b-2f45-43df-afee-620d40542853"),
DateOfBirth = new DateTime(1997, 2, 6),
EmployeeNo = "A201",
FirstName = "Lorraine",
LastName = "Shaw",
Gender = Gender.
}
);
);
}
}
}
Loading

0 comments on commit 99f1c49

Please sign in to comment.