Skip to content

Commit

Permalink
update to p19
Browse files Browse the repository at this point in the history
  • Loading branch information
Surbowl committed Jan 14, 2020
1 parent 9453ef7 commit 362bef3
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 7 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>
包含课程中搭建的项目与部分笔记,此项目的内容更新到视频 P18 。
包含课程中搭建的项目与部分笔记,目前覆盖到视频 P19
<br><br><br>
非常感谢杨老师 🤗
<br>
5 changes: 3 additions & 2 deletions Routine/Routine.APi/Controllers/CompaniesController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Routine.APi.DtoParameters;
using Routine.APi.Models;
using Routine.APi.Services;
using System;
Expand Down Expand Up @@ -78,9 +79,9 @@ public CompaniesController(ICompanyRepository companyRepository,IMapper mapper)

[HttpGet]
[HttpHead] //添加对 Http Head 的支持,使用 Head 请求时不会返回 Body
public async Task<IActionResult> GetCompanies() //Task<IActionResult> = Task<ActionResult<List<CompanyDto>>>
public async Task<IActionResult> GetCompanies([FromQuery]CompanyDtoParameters parameters) //Task<IActionResult> = Task<ActionResult<List<CompanyDto>>>
{
var companies = await _companyRepository.GetCompaniesAsync();
var companies = await _companyRepository.GetCompaniesAsync(parameters);

//不使用 AutoMapper
//var companyDtos = new List<CompanyDto>();
Expand Down
13 changes: 13 additions & 0 deletions Routine/Routine.APi/DtoParameters/CompanyDtoParameters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Routine.APi.DtoParameters
{
public class CompanyDtoParameters
{
public string Name { get; set; }
public string SearchTerm { get; set; }
}
}
1 change: 1 addition & 0 deletions Routine/Routine.APi/Models/CompanyDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ public class CompanyDto
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Introduction { get; set; }
}
}
22 changes: 20 additions & 2 deletions Routine/Routine.APi/Services/CompanyRepository.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.EntityFrameworkCore;
using Routine.APi.Data;
using Routine.APi.DtoParameters;
using Routine.APi.Entities;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -82,9 +83,26 @@ public async Task<Company> GetCompanyAsync(Guid companyId)
return await _context.Companies.FirstOrDefaultAsync(x => x.Id == companyId);
}

public async Task<IEnumerable<Company>> GetCompaniesAsync()
public async Task<IEnumerable<Company>> GetCompaniesAsync(CompanyDtoParameters parameters)
{
return await _context.Companies.ToListAsync();
if (parameters == null)
{
throw new ArgumentNullException(nameof(parameters));
}

var queryExpression = _context.Companies as IQueryable<Company>;
if (!string.IsNullOrWhiteSpace(parameters.Name))
{
parameters.Name = parameters.Name.Trim();
queryExpression = queryExpression.Where(x => x.Name == parameters.Name);
}
if (!string.IsNullOrWhiteSpace(parameters.SearchTerm))
{
parameters.SearchTerm = parameters.SearchTerm.Trim();
queryExpression = queryExpression.Where(x => x.Name.Contains(parameters.SearchTerm)
|| x.Introduction.Contains(parameters.SearchTerm));
}
return await queryExpression.ToListAsync();
}

public async Task<IEnumerable<Company>> GetCompaniesAsync(IEnumerable<Guid> companyIds)
Expand Down
5 changes: 3 additions & 2 deletions Routine/Routine.APi/Services/ICompanyRepository.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Routine.APi.Entities;
using Routine.APi.DtoParameters;
using Routine.APi.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -8,7 +9,7 @@ namespace Routine.APi.Services
{
public interface ICompanyRepository
{
Task<IEnumerable<Company>> GetCompaniesAsync();
Task<IEnumerable<Company>> GetCompaniesAsync(CompanyDtoParameters parameters);
Task<Company> GetCompanyAsync(Guid companyId);
Task<IEnumerable<Company>> GetCompaniesAsync(IEnumerable<Guid> companyIds);
void AddCompany(Company company);
Expand Down

0 comments on commit 362bef3

Please sign in to comment.