这是我的代码,我想从存储库方法中创建API。
这是我代码的Entity表:
public partial class Course
{
public int ID { get; set; }
public string Name { get; set; }
public int DepartmentID { get; set; }
[ForeignKey("DepartmentID")]
public virtual Department Department { get; set; }
public int GradeLevelsID { get; set; }
[ForeignKey("GradeLevelsID")]
public virtual GradeLevels GradeLevels { get; set; }
// Navigation
public virtual ICollection<Units> Units { get; set; }
}
根据方法,我需要一些输出:
获取课程的所有单元 我在IRepository中为以下条件编写了代码
Public interface ICourseRepository
{
Task<Course> GetAllCourseByGradeLevleId(int id)
Task<Course> UpdateCoursetAsync(int Id);
Task<Action> CreateCourseAsync(Course Course);
Task<Course> DeleteCourseAsync(int Id);
}
存储库方法如下:
public class CountryRepository : ICourseRepository
{
public Task<Action> CreateCourseAsync(Course Course)
{
throw new NotImplementedException();
}
public Task<Course> DeleteCourseAsync(int Id)
{
throw new NotImplementedException();
}
public Task<Course> GetAllCourseByGradeLevleId(int id)
{
throw new NotImplementedException();
}
public Task<Course> UpdateCoursetAsync(int Id)
{
throw new NotImplementedException();
}
我的问题是我无法编写返回类型方法,也无法从关系表中获取数据,也无法针对这种情况编写POST和PUT api。
这是我的控制器类:
[Route("api/[controller]")]
[ApiController]
public class CourseController : ControllerBase
{
private readonly ICourseRepository _courseRespository;
public CourseController(ICourseRepository courseRespository)
{
_courseRespository = courseRespository;
}
[HttpGet]
public async Task<IEnumerable<Course>> Get()
{
return await _courseRespository.GetAllCourseAsync();
}
public async Task<ActionResult<Course>> GetId(int id)
{
var result = await _courseRespository.GetAllCourseByIdAsync(id);
if (result == null)
{
return NotFound();
}
return result;
}
[HttpPost]
public async Task<ActionResult> Post(Course course)
{
// _courseRespository.CreateCourseAsync();
// await _courseRespository.SaveChangesAsync();
return CreatedAtAction("GetId", new { id = course.ID }, course);
}
在这种情况下如何编写PUT和POST。
答案 0 :(得分:-1)
尝试使用如下代码,
[Route("Course")]
public class CountryRepository : ICourseRepository
{
[Route("~/users/Create")]
[ResponseType(typeof(Course))]
[HttpPost]
public async Task<Action> CreateCourseAsync(Course Course)
{
return Request.CreateResponse(HttpStatusCode.OK, resultObj);
}
}