.net 6 API for PBSA
VIPS
Posted on April 26, 2023
Or you can create separate Project for vue.
- Migration into API Project.
- Create Return folder under project.
Create TutorialReturn.cs file under Return folder.
Below is example. Make sure if may be null then add ?.-
Create Service folder
- 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);
2. Create class as TutorialServices.cs
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;
}
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)
{
}
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");
Appsetting.json
{
"AllowedHosts": "*",
"ConnectionStrings": {
"People": "Data Source=DT13\\SQLEXPRESS2019;Initial Catalog=People;Integrated Security=True;TrustServerCertificate=True;"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
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"));
}
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;
}
}
}
💖 💪 🙅 🚩
VIPS
Posted on April 26, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.