The second day I can not solve the problem in the project: Error in requests GET/POST.
An unhandled exception occurred while processing the request.
InvalidOperationException: Unable to resolve service for type ‘RESTfull.Infrastructure.Interfaces.IStudentRepository’ while attempting to activate ‘RESTfull.API.Controllers.StudentController’.
I would be very grateful for help in indicating where I have a mistake!
For educational purposes, I make applications for recording attendance of classes
The solution consists of three projects API, Domain, Infrastructure.
I created a data model, configured the migration, it is executed successfully and creates empty tables for now (this is visible through the SQL object browser)
Next, I began to develop repositories, controllers and interfaces.
Started with the entity “Students”
Created files:
StudentRepository:
using Microsoft.EntityFrameworkCore;
using RESTfull.Domain.Model;
using RESTfull.Infrastructure.Data;
using RESTfull.Infrastructure.Interfaces;
namespace RESTfull.Infrastructure.Repositories
{
public class StudentRepository : IStudentRepository
{
private readonly Context _context;
public StudentRepository(Context context)
{
_context = context;
}
public async Task<IEnumerable<Student>> GetAllStudentsAsync()
{
return await _context.Students.Include(s => s.Group).ToListAsync();
}
public async Task<Student> GetStudentByIdAsync(int studentId)
{
return await _context.Students.Include(s => s.Group).FirstOrDefaultAsync(s => s.StudentId == studentId);
}
public async Task<Student> AddStudentAsync(Student student)
{
_context.Students.Add(student);
await _context.SaveChangesAsync();
return student;
}
public async Task<Student> UpdateStudentAsync(Student student)
{
_context.Entry(student).State = EntityState.Modified;
await _context.SaveChangesAsync();
return student;
}
public async Task<Student> DeleteStudentAsync(int studentId)
{
var student = await _context.Students.FindAsync(studentId);
if (student == null)
{
return null;
}
_context.Students.Remove(student);
await _context.SaveChangesAsync();
return student;
}
}
}
StudentController:
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using RESTfull.Domain.Model;
using RESTfull.Infrastructure.Interfaces;
using RESTfull.Infrastructure.Repositories;
namespace RESTfull.API.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class StudentController : ControllerBase
{
private readonly IStudentRepository _studentRepository;
public StudentController(IStudentRepository studentRepository)
{
_studentRepository = studentRepository;
}
[HttpGet]
public async Task<IEnumerable<Student>> GetStudents()
{
return await _studentRepository.GetAllStudentsAsync();
}
[HttpGet("{id}")]
public async Task<ActionResult<Student>> GetStudent(int id)
{
var student = await _studentRepository.GetStudentByIdAsync(id);
if (student == null)
{
return NotFound();
}
return student;
}
[HttpPost]
public async Task<ActionResult<Student>> PostStudent(Student student)
{
await _studentRepository.AddStudentAsync(student);
return CreatedAtAction("GetStudent", new { id = student.StudentId }, student);
}
[HttpPut("{id}")]
public async Task<IActionResult> PutStudent(int id, Student student)
{
if (id != student.StudentId)
{
return BadRequest();
}
await _studentRepository.UpdateStudentAsync(student);
return NoContent();
}
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteStudent(int id)
{
var student = await _studentRepository.DeleteStudentAsync(id);
if (student == null)
{
return NotFound();
}
return NoContent();
}
}
}
IStudentRepository:
using System.Collections.Generic;
using System.Threading.Tasks;
using RESTfull.Domain.Model;
namespace RESTfull.Infrastructure.Interfaces
{
public interface IStudentRepository
{
Task<IEnumerable<Student>> GetAllStudentsAsync();
Task<Student> GetStudentByIdAsync(int studentId);
Task<Student> AddStudentAsync(Student student);
Task<Student> UpdateStudentAsync(Student student);
Task<Student> DeleteStudentAsync(int studentId);
}
}
Shioniro is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.