Jump to content

C# Calling a REST Api using methods from a .net library

Igor Perrotta

Hey everyone, thanks for reading this!  I'm doing some work and i'm needing to call an rest api(All operations; POST, PUT, GET) from a .net library. This .net library will be called in WinDev. I found many tutorials and all of them use a console application and i don't know if thats applicable to using methods, since they always run in the main class.

Here is my api controller for reference:


namespace APIDevelopers.Controllers
{
    [Route("api/Developers")]
    [ApiController]
    public class DevelopersController : ControllerBase
    {
        private readonly IRepository _repository;
        private readonly IMapper _mapper;

        public DevelopersController(IRepository repository, IMapper mapper)
        {
            _repository = repository;
            _mapper = mapper;
        }

        [HttpGet]
        public ActionResult<IEnumerable<DeveloperDTO>> GetAllDevelopers()
        {
            var devReturn = _repository.GetAllDevelopers();

            return Ok(_mapper.Map<IEnumerable<DeveloperDTO>>(devReturn));
        }

        [Route("{id}", Name = "GetDeveloper")]
        [HttpGet]
        public ActionResult<DeveloperDTO> GetDeveloper(int id)
        {

            var devReturn = _repository.GetOneDeveloper(id);

            if (devReturn != null)
            {
                return Ok(_mapper.Map<DeveloperDTO>(devReturn));
            }
            return NotFound();
        }

        [HttpPost]
        public ActionResult<DeveloperDTO> postDeveloper(DeveloperDTO dev)
        {
            var devModel = _mapper.Map<Developer>(dev);

            _repository.addDeveloper(devModel);
            _repository.saveChanges();

            return CreatedAtRoute(nameof(GetDeveloper), new { Id = devModel.id }, devModel);

        }


       [Route("{id}")]
       [HttpPut]
        public ActionResult putDeveloper(int id, DeveloperDTO dev)
        {
            var developerModel = _repository.GetOneDeveloper(id);
            if (developerModel == null)

            {
                return NotFound();

            }

            _mapper.Map(dev, developerModel);

            _repository.putDeveloper(developerModel);

            _repository.saveChanges();

            return NoContent();
        }



    }
}

Thanks for the help!

Link to comment
Share on other sites

Link to post
Share on other sites

Not sure I understand your question. You want to call your API? Is the question how to call an API using WinDev (not familiar with the tool) or using C#?

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

On 2/23/2021 at 1:09 AM, Igor Perrotta said:

I found many tutorials and all of them use a console application and i don't know if thats applicable to using methods, since they always run in the main class.

The code should be the same, regardless of whether you run it from the main class or put it into classes. But in the second case you have to spend some more time to structure your code before you can use it. Tutorials typically don't do this, because all of the extra code needed to create objects etc. is a distraction from the topic at hand.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×