Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions SafeBoda/SafeBoda.Api/Controllers/DriversController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using Microsoft.AspNetCore.Mvc;
using SafeBoda.Infrastructure;
using SafeBoda.Core;

namespace SafeBoda.Api.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class DriversController : ControllerBase
{
private readonly SafeBodaDbContext _context;
public DriversController(SafeBodaDbContext context)
{
_context = context;
}

[HttpGet]
public IActionResult GetAll() => Ok(_context.Drivers.ToList());

[HttpGet("{id}")]
public IActionResult GetById(int id)
{
var driver = _context.Drivers.Find(id);
if (driver == null) return NotFound();
return Ok(driver);
}

[HttpPost]
public IActionResult Create(Driver driver)
{
_context.Drivers.Add(driver);
_context.SaveChanges();
return CreatedAtAction(nameof(GetById), new { id = driver.Id }, driver);
}

[HttpPut("{id}")]
public IActionResult Update(int id, Driver updated)
{
var driver = _context.Drivers.Find(id);
if (driver == null) return NotFound();

driver.Name = updated.Name;
driver.PhoneNumber = updated.PhoneNumber;
driver.MotoPlateNumber = updated.MotoPlateNumber;

_context.SaveChanges();
return NoContent();
}

[HttpDelete("{id}")]
public IActionResult Delete(int id)
{
var driver = _context.Drivers.Find(id);
if (driver == null) return NotFound();

_context.Drivers.Remove(driver);
_context.SaveChanges();
return NoContent();
}
}
}
60 changes: 60 additions & 0 deletions SafeBoda/SafeBoda.Api/Controllers/RidersController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using Microsoft.AspNetCore.Mvc;
using SafeBoda.Infrastructure;
using SafeBoda.Core;

namespace SafeBoda.Api.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class RidersController : ControllerBase
{
private readonly SafeBodaDbContext _context;
public RidersController(SafeBodaDbContext context)
{
_context = context;
}

[HttpGet]
public IActionResult GetAll() => Ok(_context.Riders.ToList());

[HttpGet("{id}")]
public IActionResult GetById(int id)
{
var rider = _context.Riders.Find(id);
if (rider == null) return NotFound();
return Ok(rider);
}

[HttpPost]
public IActionResult Create(Rider rider)
{
_context.Riders.Add(rider);
_context.SaveChanges();
return CreatedAtAction(nameof(GetById), new { id = rider.Id }, rider);
}

[HttpPut("{id}")]
public IActionResult Update(int id, Rider updated)
{
var rider = _context.Riders.Find(id);
if (rider == null) return NotFound();

rider.Name = updated.Name;
rider.PhoneNumber = updated.PhoneNumber;

_context.SaveChanges();
return NoContent();
}

[HttpDelete("{id}")]
public IActionResult Delete(int id)
{
var rider = _context.Riders.Find(id);
if (rider == null) return NotFound();

_context.Riders.Remove(rider);
_context.SaveChanges();
return NoContent();
}
}
}
77 changes: 77 additions & 0 deletions SafeBoda/SafeBoda.Api/Controllers/TripsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using Microsoft.AspNetCore.Mvc;
using SafeBoda.Application;
using SafeBoda.Core;

namespace SafeBoda.Api.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class TripsController : ControllerBase
{
private readonly ITripRepository _tripRepository;

public TripsController(ITripRepository tripRepository)
{
_tripRepository = tripRepository;
}

[HttpGet]
public IActionResult GetAllTrips()
{
var trips = _tripRepository.GetActiveTrips();
return Ok(trips);
}

[HttpPost("request")]
public IActionResult RequestTrip([FromBody] TripRequest request)
{
var newTrip = new Trip
{
Id = 0, // repository will assign the ID
RiderId = request.RiderId,
DriverId = request.DriverId,
StartLatitude = request.Start.Latitude,
StartLongitude = request.Start.Longitude,
EndLatitude = request.End.Latitude,
EndLongitude = request.End.Longitude,
Fare = 1000m,
RequestTime = DateTime.Now
};

var createdTrip = _tripRepository.CreateTrip(newTrip);

return CreatedAtAction(nameof(GetAllTrips), new { id = createdTrip.Id }, createdTrip);
}

[HttpPut("{id}")]
public IActionResult UpdateTrip(int id, [FromBody] Trip updatedTrip)
{
var existingTrip = _tripRepository.GetTripById(id);
if (existingTrip == null)
return NotFound();

existingTrip.RiderId = updatedTrip.RiderId;
existingTrip.DriverId = updatedTrip.DriverId;
existingTrip.StartLatitude = updatedTrip.StartLatitude;
existingTrip.StartLongitude = updatedTrip.StartLongitude;
existingTrip.EndLatitude = updatedTrip.EndLatitude;
existingTrip.EndLongitude = updatedTrip.EndLongitude;
existingTrip.Fare = updatedTrip.Fare;

_tripRepository.UpdateTrip(existingTrip);
return NoContent();
}

[HttpDelete("{id}")]
public IActionResult DeleteTrip(int id)
{
var success = _tripRepository.DeleteTrip(id);
if (!success)
return NotFound();

return NoContent();
}
}

public record TripRequest(int RiderId, int DriverId, Location Start, Location End);
}
33 changes: 33 additions & 0 deletions SafeBoda/SafeBoda.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using SafeBoda.Application;
using SafeBoda.Core;
using SafeBoda.Infrastructure;
using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(); // swager enablering

// registering repositories
builder.Services.AddScoped<ITripRepository, EfTripRepository>();


var conn = builder.Configuration.GetConnectionString("SafeBodaDb");
builder.Services.AddDbContext<SafeBodaDbContext>(options =>
options.UseSqlServer(conn));

var app = builder.Build();


if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
41 changes: 41 additions & 0 deletions SafeBoda/SafeBoda.Api/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:48696",
"sslPort": 44368
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5112",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7279;http://localhost:5112",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
26 changes: 26 additions & 0 deletions SafeBoda/SafeBoda.Api/SafeBoda.Api.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.20" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\SafeBoda.Application\SafeBoda.Application.csproj" />
<ProjectReference Include="..\SafeBoda.Core\SafeBoda.Core.csproj" />
<ProjectReference Include="..\SafeBoda.Infrastructure\SafeBoda.Infrastructure.csproj" />
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions SafeBoda/SafeBoda.Api/SafeBoda.Api.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@SafeBoda.Api_HostAddress = http://localhost:5112

GET {{SafeBoda.Api_HostAddress}}/weatherforecast/
Accept: application/json

###
8 changes: 8 additions & 0 deletions SafeBoda/SafeBoda.Api/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
12 changes: 12 additions & 0 deletions SafeBoda/SafeBoda.Api/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"SafeBodaDb": "Server=(localdb)\\MSSQLLocalDB;Database=SafeBodaDb;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading