Commit fd4c8de9 authored by cann-alberto's avatar cann-alberto
Browse files

Release pre-MMM-API

parent ae7866c4
...@@ -7,13 +7,65 @@ namespace MMM_Server.Controllers; ...@@ -7,13 +7,65 @@ namespace MMM_Server.Controllers;
[ApiController] [ApiController]
[Route("api/[controller]")] [Route("api/[controller]")]
public class ActivityController: ControllerBase public class ActivityController: ControllerBase
{ {
private readonly UserService _usersService;
public ActivityController()
public ActivityController(UserService usersService)
{ {
_usersService = usersService;
} }
[HttpGet("users")]
public async Task<List<User>> Get() =>
await _usersService.GetAsync();
[HttpGet("user/humanid/{humanId}")]
public async Task<IActionResult> GetUserByHumanId(string humanId)
{
try
{
// Call the service method to retrieve the account
var user = await _usersService.GetUserByHumanIdAsync(humanId);
if (user == null)
{
// If no account is found, return a NotFound status
return NotFound($"User with HumanID {humanId} not found.");
}
// Return the account details
return Ok(user);
}
catch (Exception ex)
{
// Handle any potential exceptions
return BadRequest($"An error occurred while retrieving the account: {ex.Message}");
}
}
[HttpPut("users/{userId}/comInfo")]
public async Task<IActionResult> UpdateAccount(string userId, User updatedUser)
{
try
{
// Call the AccountService to update the persona in the account
await _usersService.UpdateAsync(userId, updatedUser);
// Return a success response
return Ok($"Account with ID {updatedUser.UserID} was successfully updated");
}
catch (Exception ex)
{
// Return a bad request with the error message if something fails
return BadRequest($"An error occurred while updating the user {userId}: {ex.Message}");
}
}
} }
......
...@@ -30,43 +30,6 @@ public class RegistrationController : ControllerBase ...@@ -30,43 +30,6 @@ public class RegistrationController : ControllerBase
_accountsService = accountsService; _accountsService = accountsService;
} }
[HttpGet("profiles")]
public async Task<List<PersonalProfile>> Get() =>
await _personalProfilesService.GetAsync();
[HttpPost("profiles")]
public async Task<IActionResult> Post(PersonalProfile newPersonalProfile)
{
// Insert the new profile in the DB
await _personalProfilesService.CreateAsync(newPersonalProfile);
return CreatedAtAction(nameof(Get), new { id = newPersonalProfile.PersonalProfileID }, newPersonalProfile);
}
[HttpPost ("users")]
public async Task<IActionResult> Post(User newUser)
{
await _usersService.CreateAsync(newUser);
return CreatedAtAction(nameof(Get), new { id = newUser.Id }, newUser);
}
//[HttpPost("personae")]
//public async Task<IActionResult> Post(Persona newPersona)
//{
// await _personaeService.CreateAsync(newPersona);
// return CreatedAtAction(nameof(Get), new { id = newPersona.PersonaID }, newPersona);
//}
//[HttpPost("devices")]
//public async Task<IActionResult> Post(Device newDevice)
//{
// await _devicesService.CreateAsync(newDevice);
// return CreatedAtAction(nameof(Get), new { id = newDevice.Id }, newDevice);
//}
[HttpGet("accounts/{humanId}")] [HttpGet("accounts/{humanId}")]
public async Task<IActionResult> GetAccountByHumanId(string humanId) public async Task<IActionResult> GetAccountByHumanId(string humanId)
{ {
...@@ -98,6 +61,42 @@ public class RegistrationController : ControllerBase ...@@ -98,6 +61,42 @@ public class RegistrationController : ControllerBase
return CreatedAtAction(nameof(Get), new { id = newAccount.AccountID }, newAccount); return CreatedAtAction(nameof(Get), new { id = newAccount.AccountID }, newAccount);
} }
[HttpPut("accounts/{accountId}/persona")]
public async Task<IActionResult> UpdatePersonaForAccount(string accountId, Persona updatedPersona)
{
try
{
// Call the AccountService to update the persona in the account
await _accountsService.UpdateAsync(accountId, updatedPersona);
// Return a success response
return Ok($"Persona with ID {updatedPersona.PersonaID} was successfully updated for account {accountId}.");
}
catch (Exception ex)
{
// Return a bad request with the error message if something fails
return BadRequest($"An error occurred while updating the persona for account {accountId}: {ex.Message}");
}
}
[HttpPut("accounts/{accountId}")]
public async Task<IActionResult> UpdateAccount(string accountId, Account updatedAccount)
{
try
{
// Call the AccountService to update the persona in the account
await _accountsService.UpdateAsync(accountId, updatedAccount);
// Return a success response
return Ok($"Account with ID {updatedAccount.AccountID} was successfully updated");
}
catch (Exception ex)
{
// Return a bad request with the error message if something fails
return BadRequest($"An error occurred while updating the account {accountId}: {ex.Message}");
}
}
[HttpGet("avatars/{name}")] [HttpGet("avatars/{name}")]
public IActionResult GetAvatar(string name, [FromQuery] string format = "glb") public IActionResult GetAvatar(string name, [FromQuery] string format = "glb")
...@@ -160,39 +159,42 @@ public class RegistrationController : ControllerBase ...@@ -160,39 +159,42 @@ public class RegistrationController : ControllerBase
} }
} }
[HttpPut("accounts/{accountId}/persona")] [HttpGet("profiles")]
public async Task<IActionResult> UpdatePersonaForAccount(string accountId, Persona updatedPersona) public async Task<List<PersonalProfile>> Get() =>
{ await _personalProfilesService.GetAsync();
try
{
// Call the AccountService to update the persona in the account
await _accountsService.UpdateAsync(accountId, updatedPersona);
// Return a success response [HttpPost("profiles")]
return Ok($"Persona with ID {updatedPersona.PersonaID} was successfully updated for account {accountId}."); public async Task<IActionResult> Post(PersonalProfile newPersonalProfile)
} {
catch (Exception ex) // Insert the new profile in the DB
{ await _personalProfilesService.CreateAsync(newPersonalProfile);
// Return a bad request with the error message if something fails
return BadRequest($"An error occurred while updating the persona for account {accountId}: {ex.Message}"); return CreatedAtAction(nameof(Get), new { id = newPersonalProfile.PersonalProfileID }, newPersonalProfile);
}
} }
[HttpPut("accounts/{accountId}")] [HttpPost ("users")]
public async Task<IActionResult> UpdateAccount(string accountId, Account updatedAccount) public async Task<IActionResult> Post(User newUser)
{ {
try await _usersService.CreateAsync(newUser);
{
// Call the AccountService to update the persona in the account
await _accountsService.UpdateAsync(accountId, updatedAccount);
// Return a success response return CreatedAtAction(nameof(Get), new { id = newUser.UserID }, newUser);
return Ok($"Account with ID {updatedAccount.AccountID} was successfully updated");
}
catch (Exception ex)
{
// Return a bad request with the error message if something fails
return BadRequest($"An error occurred while updating the account {accountId}: {ex.Message}");
}
} }
//[HttpPost("personae")]
//public async Task<IActionResult> Post(Persona newPersona)
//{
// await _personaeService.CreateAsync(newPersona);
// return CreatedAtAction(nameof(Get), new { id = newPersona.PersonaID }, newPersona);
//}
//[HttpPost("devices")]
//public async Task<IActionResult> Post(Device newDevice)
//{
// await _devicesService.CreateAsync(newDevice);
// return CreatedAtAction(nameof(Get), new { id = newDevice.Id }, newDevice);
//}
} }
\ No newline at end of file
...@@ -7,6 +7,10 @@ namespace MMM_Server.Models ...@@ -7,6 +7,10 @@ namespace MMM_Server.Models
{ {
[BsonId] [BsonId]
[BsonRepresentation(BsonType.ObjectId)] [BsonRepresentation(BsonType.ObjectId)]
public string? Id { get; set; } public string? UserID { get; set; }
public string HumanID { get; set; } = null!;
public string? ComIp { get; set; }
public string? ComPort { get; set; }
} }
} }
using MMM_Server.Models; using MMM_Server.Models;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using MongoDB.Driver;
namespace MMM_Server.Services; namespace MMM_Server.Services;
...@@ -10,4 +11,34 @@ public class UserService : MongoDbService<User> ...@@ -10,4 +11,34 @@ public class UserService : MongoDbService<User>
{ {
} }
public async Task UpdateAsync(string id, User updatedItem)
{
// Filter to find the account by its AccountID
var filter = Builders<User>.Filter.Eq(user => user.UserID, id);
// Find the account
var user = await _collection.Find(filter).FirstOrDefaultAsync();
if (user == null)
{
throw new Exception($"User with ID {id} not found.");
}
// Update the Account
var result = await _collection.ReplaceOneAsync(filter, updatedItem);
// Throw exception if the update fails
if (result.MatchedCount == 0)
{
throw new Exception($"Failed to update the user with ID {id}.");
}
}
public async Task<User> GetUserByHumanIdAsync(string humanId)
{
// Build the filter to query the database for the HumanID
var filter = Builders<User>.Filter.Eq(user => user.HumanID, humanId);
// Fetch the first matching account or return null if not found
return await _collection.Find(filter).FirstOrDefaultAsync();
}
} }
[ [
{ {
"ContainingType": "MMM_Server.Controllers.ActivityController", "ContainingType": "MMM_Server.Controllers.ActivityController",
"Method": "GetActions", "Method": "GetUserByHumanId",
"RelativePath": "api/Activity/logins", "RelativePath": "api/Activity/user/humanid/{humanId}",
"HttpMethod": "GET", "HttpMethod": "GET",
"IsController": true, "IsController": true,
"Order": 0, "Order": 0,
"Parameters": [],
"ReturnTypes": [
{
"Type": "System.Collections.Generic.List\u00601[[MMM_Server.Models.ActionRequest, MMM-Server, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]",
"MediaTypes": [
"text/plain",
"application/json",
"text/json"
],
"StatusCode": 200
}
]
},
{
"ContainingType": "MMM_Server.Controllers.ActivityController",
"Method": "Post",
"RelativePath": "api/Activity/logins",
"HttpMethod": "POST",
"IsController": true,
"Order": 0,
"Parameters": [ "Parameters": [
{ {
"Name": "newAction", "Name": "humanId",
"Type": "MMM_Server.Models.ActionRequest", "Type": "System.String",
"IsRequired": true "IsRequired": true
} }
], ],
...@@ -56,24 +36,25 @@ ...@@ -56,24 +36,25 @@
] ]
}, },
{ {
"ContainingType": "MMM_Server.Controllers.AuthorController", "ContainingType": "MMM_Server.Controllers.ActivityController",
"Method": "Get", "Method": "UpdateAccount",
"RelativePath": "api/Author/users", "RelativePath": "api/Activity/users/{userId}/comInfo",
"HttpMethod": "GET", "HttpMethod": "PUT",
"IsController": true, "IsController": true,
"Order": 0, "Order": 0,
"Parameters": [], "Parameters": [
"ReturnTypes": [
{ {
"Type": "System.Collections.Generic.List\u00601[[MMM_Server.Models.User, MMM-Server, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]", "Name": "userId",
"MediaTypes": [ "Type": "System.String",
"text/plain", "IsRequired": true
"application/json", },
"text/json" {
], "Name": "updatedUser",
"StatusCode": 200 "Type": "MMM_Server.Models.User",
"IsRequired": true
} }
] ],
"ReturnTypes": []
}, },
{ {
"ContainingType": "MMM_Server.Controllers.CommunicationController", "ContainingType": "MMM_Server.Controllers.CommunicationController",
...@@ -242,38 +223,6 @@ ...@@ -242,38 +223,6 @@
], ],
"ReturnTypes": [] "ReturnTypes": []
}, },
{
"ContainingType": "MMM_Server.Controllers.RegistrationController",
"Method": "Post",
"RelativePath": "api/Registration/devices",
"HttpMethod": "POST",
"IsController": true,
"Order": 0,
"Parameters": [
{
"Name": "newDevice",
"Type": "MMM_Server.Models.Device",
"IsRequired": true
}
],
"ReturnTypes": []
},
{
"ContainingType": "MMM_Server.Controllers.RegistrationController",
"Method": "Post",
"RelativePath": "api/Registration/personae",
"HttpMethod": "POST",
"IsController": true,
"Order": 0,
"Parameters": [
{
"Name": "newPersona",
"Type": "MMM_Server.Models.Persona",
"IsRequired": true
}
],
"ReturnTypes": []
},
{ {
"ContainingType": "MMM_Server.Controllers.RegistrationController", "ContainingType": "MMM_Server.Controllers.RegistrationController",
"Method": "Get", "Method": "Get",
......
...@@ -14,7 +14,7 @@ using System.Reflection; ...@@ -14,7 +14,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("MMM-Server")] [assembly: System.Reflection.AssemblyCompanyAttribute("MMM-Server")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+6b5027c433ea6937ea9687f88fe2b87e947c15ff")] [assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+ae7866c4acb32e2378b42a702fff8bf2a07c2bc7")]
[assembly: System.Reflection.AssemblyProductAttribute("MMM-Server")] [assembly: System.Reflection.AssemblyProductAttribute("MMM-Server")]
[assembly: System.Reflection.AssemblyTitleAttribute("MMM-Server")] [assembly: System.Reflection.AssemblyTitleAttribute("MMM-Server")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
......
77938c610c9a74820497c30fa374e57a27ed464723429730aeb5fb34c769dcb7 7508b24e5858470743a3d1f4f711aa3a4a028eb653109d33ea19638392f7427f
{"documents":{"C:\\Users\\lab2a\\Documents\\Visual Studio 2022\\Projects\\MPAI-MMM\\MMM-Server\\*":"https://raw.githubusercontent.com/cann-alberto/MPAI-MMM/6b5027c433ea6937ea9687f88fe2b87e947c15ff/*"}} {"documents":{"C:\\Users\\lab2a\\Documents\\Visual Studio 2022\\Projects\\MPAI-MMM\\MMM-Server\\*":"https://raw.githubusercontent.com/cann-alberto/MPAI-MMM/ae7866c4acb32e2378b42a702fff8bf2a07c2bc7/*"}}
\ No newline at end of file \ 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