DEV Community

VIPS
VIPS

Posted on

.net 6 API for PBSA

Or you can create separate Project for vue.

  1. Migration into API Project.
  2. Create Return folder under project.
  3. Create TutorialReturn.cs file under Return folder.
    Below is example. Make sure if may be null then add ?.

  4. Create Service folder

    1. Create iterface ITutorialServices.cs file

        List<TutorialReturn> GetTutorialList();
        TutorialReturn GetTutorialById(int id);
// Here if you want to return all data then use below one.
        //TblTutorial GetTutorialById(int id);
        int CreateTutorial(TblTutorial Tutorial);
        int UpdateTutorial (TblTutorial Tutorial);
        int DeleteTutorial (int id);
Enter fullscreen mode Exit fullscreen mode
2. Create class as TutorialServices.cs
Enter fullscreen mode Exit fullscreen mode
public class TutorialServices : ITutorialServices

    {
        public int CreateTutorial(TblTutorial Tutorial)
        {
            throw new NotImplementedException();
        }

        public int DeleteTutorial(int id)
        {
            throw new NotImplementedException();
        }

        public TutorialReturn GetTutorialById(int id)
        {
            TutorialReturn TutorialReturn = new TutorialReturn();
            try
            {
                using (var db = new PeopleContext())
                {
                   TutorialReturn =  (from x in db.TblTutorials
                                        where x.Id == id
                                        select new TutorialReturn
                                        {
                                            Id = x.Id,
                                            Address = x.Address,
                                            BirthDate = x.BirthDate,
                                            Email = x.Email,
                                            GivenName = x.GivenName,
                                            PhoneNumber = x.PhoneNumber,
                                            Surname = x.Surname
                                        }).FirstOrDefault();
                    return TutorialReturn;
                }

            }
            catch (Exception ex)
            {
                return TutorialReturn;
            }

        }

        //public TblTutorial GetTutorialById(int id)
        //{
        //    try
        //    {
        //        using (var db = new PeopleContext())
        //        {
        //            var result = db.TblTutorials.Where(x => x.Id == id).FirstOrDefault();

        //            return result;
        //        }

        //    }
        //    catch (Exception ex)
        //    {
        //        return null;
        //    }

        //}

        public List<TutorialReturn> GetTutorialList()
        {
            using (var db = new PeopleContext())
            {
                var lstTutorial = (from x in db.TblTutorials
                                    select new TutorialReturn
                                    {
                                        Id = x.Id,
                                        Address = x.Address,
                                        BirthDate = x.BirthDate,
                                        Email = x.Email,
                                        GivenName = x.GivenName,
                                        PhoneNumber = x.PhoneNumber,
                                        Surname = x.Surname
                                    }).Take(50).ToList();
                return lstTutorial;
            } 
        }

        public int UpdateTutorial(TblTutorial Tutorial)
        {
            if (Tutorial != null)
            {
                using (var udb = new PeopleContext())
                {
                    var update = udb.TblTutorials.Where(o => o.Id == Tutorial.Id).FirstOrDefault();
                    if (update != null)
                    {
                        update.Surname = Tutorial.Surname;
                        update.GivenName = Tutorial.GivenName;
                        update.Email = Tutorial.Email;
                        update.BirthDate = Tutorial.BirthDate;
                        update.PhoneNumber = Tutorial.PhoneNumber;
                        update.Address = Tutorial.Address;
                        udb.Update(update);
                        udb.SaveChanges();
                    }

                    return Tutorial.Id;
                }
            }
            return Tutorial.Id;
        }
Enter fullscreen mode Exit fullscreen mode

Then create New controller as Tutorial

[Route("api/[controller]")]
    [ApiController]
    public class TutorialController : ControllerBase
    {
        private ITutorialServices TutorialServices;

        public TutorialController(ITutorialServices TutorialServices)
        {
            this.TutorialServices = TutorialServices;
        }


        // GET: api/<TutorialController>
        [HttpGet]
        public IEnumerable<TutorialReturn> Get()
        {
            return TutorialServices.GetTutorialList();
        }

        [HttpGet("{id}")]
        public TutorialReturn GetTutorialByID(int id)
        {
            return TutorialServices.GetTutorialById(id);
        }

        // GET api/<TutorialController>/5
        //[HttpGet("{id}")]
        //public TblTutorial GetTutorialbyID(int id)
        //{
        //    return TutorialServices.GetTutorialById(id);
        //}

        // POST api/<TutorialController>
        [HttpPost]
        public void Post([FromBody] string value)
        {
        }

        // PUT api/<TutorialController>/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] TblTutorial value)
        {
             TutorialServices.UpdateTutorial(value);
        }

        // DELETE api/<TutorialController>/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }
Enter fullscreen mode Exit fullscreen mode

Also add below thing into Program.cs file

// Add services to the container.\
builder.Services.AddDbContext<PeopleContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("People")));

builder.Services.AddScoped<ITutorialService, TutorialService>();
builder.Services.AddScoped<PeopleContext, PeopleContext>();

builder.Services.AddCors(options =>
{
    options.AddPolicy("AllOrigins",
        builder =>
        {
            builder.AllowAnyHeader()
                           .AllowAnyOrigin()
                          .AllowAnyMethod();
        });
});


app.UseCors("AllOrigins");


Enter fullscreen mode Exit fullscreen mode

Appsetting.json

{
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "People": "Data Source=DT13\\SQLEXPRESS2019;Initial Catalog=People;Integrated Security=True;TrustServerCertificate=True;"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  }
}

Enter fullscreen mode Exit fullscreen mode

PeopleContext file replace with below thing


    protected readonly IConfiguration Configuration;
    //public PeopleContext()
    //{
    //}

    //public PeopleContext(DbContextOptions<PeopleContext> options)
    //    : base(options)
    //{
    //}
    public PeopleContext(IConfiguration configuration)
    {
        Configuration = configuration;
    }
    protected override void OnConfiguring(DbContextOptionsBuilder options)
    {
        options.UseSqlServer(Configuration.GetConnectionString("People"));
    }

Enter fullscreen mode Exit fullscreen mode

Updated service with new connection string thing.


    public class TutorialServices : ITutorialServices

    {
        private readonly PeopleContext _dbContext;
        public TutorialServices(PeopleContext dbContext)
        {
            _dbContext = dbContext;
        }


        public TutorialReturn GetTutorialById(int id)
        {

            get data from database

        }

        public List<TutorialReturn> GetTutorialList()
        {
            var lstTutorial = (from x in _dbContext.TblTutorials
                                select new TutorialReturn
                                {
                                    Id = x.Id,
                                    Address = x.Address,
                                    BirthDate = x.BirthDate,
                                    Email = x.Email,
                                    GivenName = x.GivenName,
                                    PhoneNumber = x.PhoneNumber,
                                    Surname = x.Surname
                                }).Take(50).ToList();
            return lstTutorial;

        }

        public int UpdateTutorial(TblTutorial Tutorial)
        {

            var update = _dbContext.TblTutorials.Where(o => o.Id == Tutorial.Id).FirstOrDefault();
            if (update != null)
            {
                update.Surname = Tutorial.Surname;
                update.GivenName = Tutorial.GivenName;
                update.Email = Tutorial.Email;
                update.BirthDate = Tutorial.BirthDate;
                update.PhoneNumber = Tutorial.PhoneNumber;
                update.Address = Tutorial.Address;
                _dbContext.Update(update);
                _dbContext.SaveChanges();
            }

            return Tutorial.Id;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
kinjal_lahar_8d2cda75213b profile image
Info Comment hidden by post author - thread only accessible via permalink
Kinjal lahar

no working

Collapse
 
kinjal_lahar_8d2cda75213b profile image
Info Comment hidden by post author - thread only accessible via permalink
Kinjal lahar

deployement error

Some comments have been hidden by the post's author - find out more