Click here to Skip to main content
15,886,801 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want connection of swagger api with mariadb database

What I have tried:

C#
using ASP.NET_WebAPI6.DTO;
using ASP.NET_WebAPI6.Entities;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Net;

namespace ASP.NET_WebAPI6.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class UserController : ControllerBase
    {
        private readonly DBContext DBContext;

        public UserController(DBContext DBContext)
        {
            this.DBContext = DBContext;
        }

        [HttpGet("GetUsers")]
        public async Task<actionresult<list<userdto>>> Get()
        {
            var list = await DBContext.Users.Select(
                s => new UserDTO()
                {
                    Id = s.Id,
                    FirstName = s.FirstName,
                    LastName = s.LastName,
                    Username = s.Username,
                    Password = s.Password,
                    EnrollmentDate = s.EnrollmentDate
                }
            ).ToListAsync();

            if (list.Count < 0)
            {
                return NotFound();
            }
            else
            {
                return list;
            }
        }

        [HttpGet("GetUserById")]
        public async Task<actionresult<userdto>> GetUserById(int Id)
        {
            UserDTO User = await DBContext.Users.Select(
                    s => new UserDTO()
                    {
                        Id = s.Id,
                        FirstName = s.FirstName,
                        LastName = s.LastName,
                        Username = s.Username,
                        Password = s.Password,
                        EnrollmentDate = s.EnrollmentDate
                    })
                .FirstOrDefaultAsync(s => s.Id == Id);

            if (User == null)
            {
                return NotFound();
            }
            else
            {
                return User;
            }
        }

        [HttpPost("InsertUser")]
        public async Task<httpstatuscode> InsertUser(UserDTO User)
        {
            var entity = new User()
            {
                Id = User.Id,
                FirstName = User.FirstName,
                LastName = User.LastName,
                Username = User.Username,
                Password = User.Password,
                EnrollmentDate = User.EnrollmentDate
            };

            DBContext.Users.Add(entity);
            await DBContext.SaveChangesAsync();

            return HttpStatusCode.Created;
        }

        [HttpPut("UpdateUser")]
        public async Task<httpstatuscode> UpdateUser(UserDTO User)
        {
            var entity = await DBContext.Users.FirstOrDefaultAsync(s => s.Id == User.Id);

            entity.FirstName = User.FirstName;
            entity.LastName = User.LastName;
            entity.Username = User.Username;
            entity.Password = User.Password;
            entity.EnrollmentDate = User.EnrollmentDate;

            await DBContext.SaveChangesAsync();
            return HttpStatusCode.OK;
        }

        [HttpDelete("DeleteUser/{Id}")]
        public async Task<httpstatuscode> DeleteUser(int Id)
        {
            var entity = new User()
            {
                Id = Id
            };
            DBContext.Users.Attach(entity);
            DBContext.Users.Remove(entity);
            await DBContext.SaveChangesAsync();
            return HttpStatusCode.OK;
        }
    }
}
Posted
Updated 1-Nov-22 2:22am
v2
Comments
Richard Deeming 1-Nov-22 8:03am    
Swagger is a technology for documenting your API. It doesn't connect to any database.

You need to explain precisely what the error is, and on which line of code it occurs.
Richard Deeming 1-Nov-22 8:04am    
Also, you seem to be storing your users' passwords in plain text. Don't do that.

Secure Password Authentication Explained Simply[^]

 
Share this answer
 
Like Richard said, the error you're implying has nothing to do with Swagger. Swagger is an interface that allows you to document and test your API. It has nothing to do with your database or how you access it.

The error means you have a field in your database record that is NULL, which means a value does not exist. NULL cannot be converted to another type, like an integer or date. You should take a look in your Users table to see where you have NULLs and fix your code that creates users to avoid leaving those values empty.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900