Commit 74d009de authored by cann-alberto's avatar cann-alberto
Browse files

CRUD operations for Profiles

parent 1b8064fe
using MMM_Server.Models;
using MMM_Server.Services;
using Microsoft.AspNetCore.Mvc;
namespace MMM_Server.Controllers;
[ApiController]
[Route("api/[controller]/profiles")]
public class RegistrationController : ControllerBase
{
private readonly ProfileService _profilesService;
public RegistrationController(ProfileService profilesService) =>
_profilesService = profilesService;
[HttpGet]
public async Task<List<Profile>> Get() =>
await _profilesService.GetAsync();
[HttpGet("{id:length(24)}")]
public async Task<ActionResult<Profile>> Get(string id)
{
var book = await _profilesService.GetAsync(id);
if (book is null)
{
return NotFound();
}
return book;
}
[HttpPost]
public async Task<IActionResult> Post(Profile newProfile)
{
await _profilesService.CreateAsync(newProfile);
return CreatedAtAction(nameof(Get), new { id = newProfile.Id }, newProfile);
}
[HttpPut("{id:length(24)}")]
public async Task<IActionResult> Update(string id, Profile updatedProfile)
{
var profile = await _profilesService.GetAsync(id);
if (profile is null)
{
return NotFound();
}
updatedProfile.Id = profile.Id;
await _profilesService.UpdateAsync(id, updatedProfile);
return NoContent();
}
[HttpDelete("{id:length(24)}")]
public async Task<IActionResult> Delete(string id)
{
var profile = await _profilesService.GetAsync(id);
if (profile is null)
{
return NotFound();
}
await _profilesService.RemoveAsync(id);
return NoContent();
}
}
namespace MMM_Server.Controllers
{
public class RightController
{
}
}
using Microsoft.AspNetCore.Mvc;
namespace MMM_Server.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
}
......@@ -22,5 +22,17 @@ public class ProfileService
public async Task<List<Profile>> GetAsync() =>
await _profilesCollection.Find(_ => true).ToListAsync();
public async Task<Profile?> GetAsync(string id) =>
await _profilesCollection.Find(x => x.Id == id).FirstOrDefaultAsync();
public async Task CreateAsync(Profile newBook) =>
await _profilesCollection.InsertOneAsync(newBook);
public async Task UpdateAsync(string id, Profile updatedBook) =>
await _profilesCollection.ReplaceOneAsync(x => x.Id == id, updatedBook);
public async Task RemoveAsync(string id) =>
await _profilesCollection.DeleteOneAsync(x => x.Id == id);
}
namespace MMM_Server
{
public class WeatherForecast
{
public DateOnly Date { get; set; }
public int TemperatureC { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
public string? Summary { get; set; }
}
}
......@@ -20,16 +20,58 @@
]
},
{
"ContainingType": "MMM_Server.Controllers.WeatherForecastController",
"ContainingType": "MMM_Server.Controllers.RegistrationController",
"Method": "Get",
"RelativePath": "WeatherForecast",
"RelativePath": "api/Registration/profiles",
"HttpMethod": "GET",
"IsController": true,
"Order": 0,
"Parameters": [],
"ReturnTypes": [
{
"Type": "System.Collections.Generic.IEnumerable\u00601[[MMM_Server.WeatherForecast, MMM-Server, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]",
"Type": "System.Collections.Generic.List\u00601[[MMM_Server.Models.Profile, MMM-Server, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]",
"MediaTypes": [
"text/plain",
"application/json",
"text/json"
],
"StatusCode": 200
}
]
},
{
"ContainingType": "MMM_Server.Controllers.RegistrationController",
"Method": "Post",
"RelativePath": "api/Registration/profiles",
"HttpMethod": "POST",
"IsController": true,
"Order": 0,
"Parameters": [
{
"Name": "newProfile",
"Type": "MMM_Server.Models.Profile",
"IsRequired": true
}
],
"ReturnTypes": []
},
{
"ContainingType": "MMM_Server.Controllers.RegistrationController",
"Method": "Get",
"RelativePath": "api/Registration/profiles/{id}",
"HttpMethod": "GET",
"IsController": true,
"Order": 0,
"Parameters": [
{
"Name": "id",
"Type": "System.String",
"IsRequired": true
}
],
"ReturnTypes": [
{
"Type": "MMM_Server.Models.Profile",
"MediaTypes": [
"text/plain",
"application/json",
......@@ -37,7 +79,43 @@
],
"StatusCode": 200
}
]
},
{
"ContainingType": "MMM_Server.Controllers.RegistrationController",
"Method": "Update",
"RelativePath": "api/Registration/profiles/{id}",
"HttpMethod": "PUT",
"IsController": true,
"Order": 0,
"Parameters": [
{
"Name": "id",
"Type": "System.String",
"IsRequired": true
},
{
"Name": "updatedProfile",
"Type": "MMM_Server.Models.Profile",
"IsRequired": true
}
],
"ReturnTypes": []
},
{
"ContainingType": "MMM_Server.Controllers.RegistrationController",
"Method": "Delete",
"RelativePath": "api/Registration/profiles/{id}",
"HttpMethod": "DELETE",
"IsController": true,
"Order": 0,
"Parameters": [
{
"Name": "id",
"Type": "System.String",
"IsRequired": true
}
],
"EndpointName": "GetWeatherForecast"
"ReturnTypes": []
}
]
\ No newline at end of file
......@@ -14,7 +14,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("MMM-Server")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+26554026ff8bd17a9250b70a62d41edfd89676d4")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+1b8064fe62f388c9a5eddd312865292d6affd7a4")]
[assembly: System.Reflection.AssemblyProductAttribute("MMM-Server")]
[assembly: System.Reflection.AssemblyTitleAttribute("MMM-Server")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
......
3ad83b47bf6630c8d1b47466b8b7a8d687cfbbba2849c2c7e8b3125e78dd6169
45771de9efe7fc0804dee5922051ba0d2b4be37e04a63b840888622c0ed49947
cdf0848ca52e96756c3e1667a156c3ebf645a753ce184c1b1951b361aa74256b
94880d7acaa4b9506f48db68bb33584a67f669b37061b9f3b5f92c272b82b128
{"documents":{"C:\\Users\\lab2a\\Documents\\Visual Studio 2022\\Projects\\MPAI-MMM\\MMM-Server\\*":"https://raw.githubusercontent.com/cann-alberto/MPAI-MMM/26554026ff8bd17a9250b70a62d41edfd89676d4/*"}}
\ No newline at end of file
{"documents":{"C:\\Users\\lab2a\\Documents\\Visual Studio 2022\\Projects\\MPAI-MMM\\MMM-Server\\*":"https://raw.githubusercontent.com/cann-alberto/MPAI-MMM/1b8064fe62f388c9a5eddd312865292d6affd7a4/*"}}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment