Skip to content

Commit a85a7c5

Browse files
authored
Merge pull request #2 from chuuchuun/homework/2-creating_models_and_controllers
Created 6 models and corresponding controllers enabling CRUD operations
2 parents 5db4886 + 5347f29 commit a85a7c5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+3552
-102
lines changed

.gitignore

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
## Ignore Visual Studio user-specific files
2+
*.suo
3+
*.user
4+
*.userosscache
5+
*.sln.docstates
6+
7+
## Ignore Visual Studio 2015+ cache directory
8+
.vs/
9+
10+
## Ignore build results
11+
[Bb]in/
12+
[Oo]bj/
13+
[Ll]og/
14+
[Dd]ebug*/
15+
[Rr]elease*/
16+
17+
## Ignore test results
18+
[Tt]est[Rr]esult*/
19+
[Bb]uild[Ll]og.*
20+
21+
## Ignore NuGet packages
22+
*.nupkg
23+
/packages/
24+
*.nuget.props
25+
*.nuget.targets
26+
27+
## Ignore Click-Once publish directory
28+
publish/
29+
30+
## Ignore Azure publish settings
31+
*.publishproj
32+
*.azurePubxml
33+
#*.pubxml
34+
PublishScripts/
35+
36+
## Ignore temporary files
37+
*.tmp
38+
*.log
39+
40+
## Ignore database files
41+
*.mdf
42+
*.ldf
43+
44+
## Ignore VSCode settings (optional, if you're using VSCode too)
45+
.vscode/
46+
47+
## OS-specific files
48+
.DS_Store
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
using LabProject.Enums;
2+
using LabProject.Models;
3+
using Microsoft.AspNetCore.Mvc;
4+
5+
namespace LabProject.Controllers
6+
{
7+
[Route("api/[controller]")]
8+
[ApiController]
9+
public class AppointmentController : ControllerBase
10+
{
11+
private static List<Appointment> Appointments = new List<Appointment>
12+
{
13+
new Appointment { Id = 1, ClientId = 1, ProviderId = 1, ServiceId = 1, LocationId = 1, DateTime = DateTime.Today, Status = AppointmentStatus.Completed },
14+
new Appointment { Id = 2, ClientId = 2, ProviderId = 1, ServiceId = 2, LocationId = 1, DateTime = DateTime.Today.AddDays(1) }
15+
};
16+
17+
/// <summary>
18+
/// Gets all appointments in the system.
19+
/// </summary>
20+
/// <returns>A list of all appointments.</returns>
21+
/// <response code="200">Returns the list of all appointments.</response>
22+
[HttpGet]
23+
public ActionResult<IEnumerable<Appointment>> GetAppointments()
24+
{
25+
return Ok(Appointments);
26+
}
27+
28+
/// <summary>
29+
/// Gets a specific appointment by ID.
30+
/// </summary>
31+
/// <param name="id">The unique identifier of the appointment.</param>
32+
/// <returns>The requested appointment.</returns>
33+
/// <response code="200">Returns the appointment with the given ID.</response>
34+
/// <response code="404">No appointment found with the specified ID.</response>
35+
[HttpGet("{id}")]
36+
public ActionResult<Appointment> GetAppointmentById([FromRoute] int id)
37+
{
38+
var appointment = Appointments.FirstOrDefault(a => a.Id == id);
39+
if (appointment is null)
40+
{
41+
return NotFound();
42+
}
43+
return Ok(appointment);
44+
}
45+
46+
/// <summary>
47+
/// Creates a new appointment.
48+
/// </summary>
49+
/// <param name="appointment">The appointment object to create.</param>
50+
/// <returns>The created appointment.</returns>
51+
/// <response code="201">The appointment was created successfully.</response>
52+
[HttpPost]
53+
public ActionResult<Appointment> CreateAppointment([FromBody] Appointment appointment)
54+
{
55+
appointment.Id = Appointments.Any() ? Appointments.Max(a => a.Id) + 1 : 1;
56+
Appointments.Add(appointment);
57+
return CreatedAtAction(nameof(GetAppointmentById), new { id = appointment.Id }, appointment);
58+
}
59+
60+
/// <summary>
61+
/// Updates an existing appointment.
62+
/// </summary>
63+
/// <param name="id">The ID of the appointment to update.</param>
64+
/// <param name="updatedAppointment">The updated appointment data.</param>
65+
/// <returns>The updated appointment.</returns>
66+
/// <response code="200">The appointment was updated successfully.</response>
67+
/// <response code="404">No appointment found with the specified ID.</response>
68+
[HttpPut("{id}")]
69+
public ActionResult UpdateAppointment([FromRoute] int id, [FromBody] Appointment updatedAppointment)
70+
{
71+
var appointment = Appointments.FirstOrDefault(a => a.Id == id);
72+
if (appointment is null)
73+
{
74+
return NotFound();
75+
}
76+
77+
appointment.ClientId = updatedAppointment.ClientId;
78+
appointment.ProviderId = updatedAppointment.ProviderId;
79+
appointment.ServiceId = updatedAppointment.ServiceId;
80+
appointment.LocationId = updatedAppointment.LocationId;
81+
appointment.DateTime = updatedAppointment.DateTime;
82+
appointment.Status = updatedAppointment.Status;
83+
84+
return Ok(appointment);
85+
}
86+
87+
/// <summary>
88+
/// Deletes an appointment by ID.
89+
/// </summary>
90+
/// <param name="id">The unique identifier of the appointment to delete.</param>
91+
/// <returns>Status of the deletion.</returns>
92+
/// <response code="200">The appointment was deleted successfully.</response>
93+
/// <response code="404">No appointment found with the specified ID.</response>
94+
[HttpDelete("{id}")]
95+
public ActionResult DeleteAppointment([FromRoute] int id)
96+
{
97+
var appointment = Appointments.FirstOrDefault(a => a.Id == id);
98+
if (appointment is null)
99+
{
100+
return NotFound();
101+
}
102+
103+
Appointments.Remove(appointment);
104+
return Ok();
105+
}
106+
}
107+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
using LabProject.Models;
2+
using Microsoft.AspNetCore.Mvc;
3+
4+
namespace LabProject.Controllers
5+
{
6+
[Route("api/[controller]")]
7+
[ApiController]
8+
public class ClientController : ControllerBase
9+
{
10+
private static List<Client> Clients = new List<Client>
11+
{
12+
new Client {Id = 1, Name = "Mary Sue", EmailAddress = "[email protected]", Phone = "123456789"},
13+
new Client {Id = 2, Name = "Jane Doe", EmailAddress = "[email protected]", Phone = "987654321"}
14+
};
15+
16+
/// <summary>
17+
/// Gets all clients in the system.
18+
/// </summary>
19+
/// <returns>A list of all clients.</returns>
20+
/// <response code="200">Returns the list of all clients.</response>
21+
[HttpGet]
22+
public ActionResult<IEnumerable<Client>> GetClients()
23+
{
24+
return Ok(Clients);
25+
}
26+
27+
/// <summary>
28+
/// Gets a specific client by ID.
29+
/// </summary>
30+
/// <param name="id">The unique identifier of the client.</param>
31+
/// <returns>The requested client.</returns>
32+
/// <response code="200">Returns the client with the given ID.</response>
33+
/// <response code="404">No client found with the specified ID.</response>
34+
[HttpGet("{id}")]
35+
public ActionResult<Client> GetClientById([FromRoute] int id)
36+
{
37+
var client = Clients.FirstOrDefault(c => c.Id == id);
38+
if (client is null)
39+
{
40+
return NotFound();
41+
}
42+
return Ok(client);
43+
}
44+
45+
/// <summary>
46+
/// Creates a new client.
47+
/// </summary>
48+
/// <param name="client">The client data to create.</param>
49+
/// <returns>The created client.</returns>
50+
/// <response code="201">The client was created successfully.</response>
51+
[HttpPost]
52+
public ActionResult<Client> CreateClient([FromBody] Client client)
53+
{
54+
client.Id = Clients.Any() ? Clients.Max(c => c.Id) + 1 : 1;
55+
Clients.Add(client);
56+
return CreatedAtAction(nameof(GetClientById), new { id = client.Id }, client);
57+
}
58+
59+
/// <summary>
60+
/// Updates an existing client.
61+
/// </summary>
62+
/// <param name="id">The ID of the client to update.</param>
63+
/// <param name="client">The updated client data.</param>
64+
/// <returns>The updated client.</returns>
65+
/// <response code="200">The client was updated successfully.</response>
66+
/// <response code="404">No client found with the specified ID.</response>
67+
[HttpPut("{id}")]
68+
public ActionResult UpdateClient([FromRoute] int id, [FromBody] Client client)
69+
{
70+
var clientToUpdate = Clients.FirstOrDefault(c => c.Id == id);
71+
if (clientToUpdate is null)
72+
{
73+
return NotFound();
74+
}
75+
clientToUpdate.Name = client.Name;
76+
clientToUpdate.EmailAddress = client.EmailAddress;
77+
clientToUpdate.Phone = client.Phone;
78+
return Ok(clientToUpdate);
79+
}
80+
81+
/// <summary>
82+
/// Deletes a client by its ID.
83+
/// </summary>
84+
/// <param name="id">The unique identifier of the client to delete.</param>
85+
/// <returns>Status of the deletion.</returns>
86+
/// <response code="200">The client was deleted successfully.</response>
87+
/// <response code="404">No client found with the specified ID.</response>
88+
[HttpDelete("{id}")]
89+
public ActionResult DeleteClient([FromRoute] int id)
90+
{
91+
var client = Clients.FirstOrDefault(c => c.Id == id);
92+
if (client is null)
93+
{
94+
return NotFound();
95+
}
96+
Clients.Remove(client);
97+
return Ok();
98+
}
99+
}
100+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
using LabProject.Models;
2+
using Microsoft.AspNetCore.Mvc;
3+
4+
namespace LabProject.Controllers
5+
{
6+
[Route("api/[controller]")]
7+
[ApiController]
8+
public class LocationController : ControllerBase
9+
{
10+
private static List<Location> Locations = new List<Location>
11+
{
12+
new Location {Id = 1, Name = "Best Beauty", Address = "Main street 12", City = "Gdansk", Phone = "123456789"},
13+
new Location {Id = 2, Name = "New you", Address = "City square", City = "Warsaw", Phone = "987654321"},
14+
};
15+
16+
/// <summary>
17+
/// Gets all locations in the system.
18+
/// </summary>
19+
/// <returns>A list of all locations.</returns>
20+
/// <response code="200">Returns the list of all locations.</response>
21+
[HttpGet]
22+
public ActionResult<IEnumerable<Location>> GetLocations()
23+
{
24+
return Ok(Locations);
25+
}
26+
27+
/// <summary>
28+
/// Retrieves a specific location by its ID.
29+
/// </summary>
30+
/// <param name="id">The unique identifier of the location.</param>
31+
/// <returns>The location matching the ID.</returns>
32+
/// <response code="200">Returns the location with the specified ID.</response>
33+
/// <response code="404">No location found with the specified ID.</response>
34+
[HttpGet("{id}")]
35+
public ActionResult<Location> GetLocationById([FromRoute] int id)
36+
{
37+
var location = Locations.FirstOrDefault(l => l.Id == id);
38+
if (location is null)
39+
{
40+
return NotFound();
41+
}
42+
return Ok(location);
43+
}
44+
45+
/// <summary>
46+
/// Creates a new location.
47+
/// </summary>
48+
/// <param name="location">The location data to create.</param>
49+
/// <returns>The created location.</returns>
50+
/// <response code="201">The location was created successfully.</response>
51+
[HttpPost]
52+
public ActionResult<Location> CreateLocation([FromBody] Location location)
53+
{
54+
location.Id = Locations.Any() ? Locations.Max(l => l.Id) + 1 : 1;
55+
Locations.Add(location);
56+
return CreatedAtAction(nameof(GetLocationById), new { id = location.Id }, location);
57+
}
58+
59+
/// <summary>
60+
/// Updates an existing location.
61+
/// </summary>
62+
/// <param name="id">The ID of the location to update.</param>
63+
/// <param name="location">The updated location data.</param>
64+
/// <returns>The updated location.</returns>
65+
/// <response code="200">The location was updated successfully.</response>
66+
/// <response code="404">No location found with the specified ID.</response>
67+
[HttpPut("{id}")]
68+
public ActionResult UpdateLocation([FromRoute] int id, [FromBody] Location location)
69+
{
70+
var locationToUpdate = Locations.FirstOrDefault(l => l.Id == id);
71+
if (locationToUpdate is null)
72+
{
73+
return NotFound();
74+
}
75+
76+
locationToUpdate.Name = location.Name;
77+
locationToUpdate.Address = location.Address;
78+
locationToUpdate.City = location.City;
79+
locationToUpdate.Phone = location.Phone;
80+
81+
return Ok(locationToUpdate);
82+
}
83+
84+
/// <summary>
85+
/// Deletes a location by its ID.
86+
/// </summary>
87+
/// <param name="id">The unique identifier of the location to delete.</param>
88+
/// <returns>Status of the deletion.</returns>
89+
/// <response code="200">The location was deleted successfully.</response>
90+
/// <response code="404">No location found with the specified ID.</response>
91+
[HttpDelete("{id}")]
92+
public ActionResult DeleteLocation([FromRoute] int id)
93+
{
94+
var location = Locations.FirstOrDefault(l => l.Id == id);
95+
if (location is null)
96+
{
97+
return NotFound();
98+
}
99+
100+
Locations.Remove(location);
101+
return Ok();
102+
}
103+
}
104+
}

0 commit comments

Comments
 (0)