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

Added support to Persona: download of the 3dmodel, handling registration, data...

Added support to Persona: download of the 3dmodel, handling registration, data structure related to persona updated during the registration
parent 7d39f396
......@@ -4,10 +4,23 @@ using MMM_Server.Services;
namespace MMM_Server.Controllers;
public class ActivityController
[ApiController]
[Route("api/[controller]")]
public class ActivityController: ControllerBase
{
}
private readonly UserService _usersService;
public ActivityController(UserService usersService)
{
_usersService = usersService;
}
[HttpGet("profiles")]
public async Task<List<User>> Get() =>
await _usersService.GetAsync();
}
......
namespace MMM_Server.Controllers
namespace MMM_Server.Controllers;
public class AuthorController
{
public class AuthorController
{
}
}
......@@ -14,13 +14,20 @@ public class RegistrationController : ControllerBase
private readonly UserService _usersService;
private readonly PersonaService _personaeService;
private readonly DeviceService _devicesService;
private readonly AccountService _accountsService;
public RegistrationController(PersonalProfileService profilesService, UserService usersService, PersonaService personaeService, DeviceService devicesService)
public RegistrationController(
PersonalProfileService profilesService,
UserService usersService,
PersonaService personaeService,
DeviceService devicesService,
AccountService accountsService)
{
_personalProfilesService = profilesService;
_usersService = usersService;
_personaeService = personaeService;
_devicesService = devicesService;
_accountsService = accountsService;
}
[HttpGet("profiles")]
......@@ -30,9 +37,16 @@ public class RegistrationController : ControllerBase
[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.Id }, newPersonalProfile);
// Insert the new account in the DB
//await _accountsService.CreateAsync(newAccount);
return CreatedAtAction(nameof(Get), new { id = newPersonalProfile.PersonalProfileID }, newPersonalProfile);
}
[HttpPost ("users")]
......@@ -48,7 +62,7 @@ public class RegistrationController : ControllerBase
{
await _personaeService.CreateAsync(newPersona);
return CreatedAtAction(nameof(Get), new { id = newPersona.Id }, newPersona);
return CreatedAtAction(nameof(Get), new { id = newPersona.PersonaID }, newPersona);
}
[HttpPost("devices")]
......@@ -59,4 +73,91 @@ public class RegistrationController : ControllerBase
return CreatedAtAction(nameof(Get), new { id = newDevice.Id }, newDevice);
}
[HttpPost("accounts")]
public async Task<IActionResult> Post(Account newAccount)
{
await _accountsService.CreateAsync(newAccount);
return CreatedAtAction(nameof(Get), new { id = newAccount.AccountID }, newAccount);
}
// Parameterized GET endpoint for avatars
[HttpGet("avatars/{name}")]
public IActionResult GetAvatar(string name, [FromQuery] string format = "glb")
{
// Ensure the name is safe to use in file paths
if (string.IsNullOrWhiteSpace(name) || name.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0)
{
return BadRequest("Invalid avatar name.");
}
var avatarDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Resources", "Personae");
var glbFilePath = Path.Combine(avatarDirectory, $"{name}.glb");
var metadataFilePath = Path.Combine(avatarDirectory, $"{name}.json");
var pngFilePath = Path.Combine(avatarDirectory, $"{name}.png");
// Se il formato richiesto è "json", restituisci i metadati
if (format.ToLower() == "json")
{
if (System.IO.File.Exists(metadataFilePath))
{
var fileBytes = System.IO.File.ReadAllBytes(metadataFilePath);
return File(fileBytes, "application/json", $"{name}.json");
}
else
{
return NotFound($"Metadata file for avatar '{name}.json' not found.");
}
}
// Altrimenti, se il formato è "glb", restituisci il modello .glb
else if (format.ToLower() == "glb")
{
if (System.IO.File.Exists(glbFilePath))
{
var fileBytes = System.IO.File.ReadAllBytes(glbFilePath);
return File(fileBytes, "model/gltf-binary", $"{name}.glb");
}
else
{
return NotFound($"Avatar file '{name}.glb' not found.");
}
}
else if (format.ToLower() == "png")
{
if (System.IO.File.Exists(pngFilePath))
{
var fileBytes = System.IO.File.ReadAllBytes(pngFilePath);
return File(fileBytes, "image/png", $"{name}.png");
}
else
{
return NotFound($"Image file '{name}.png' not found.");
}
}
else
{
return BadRequest("Invalid format specified. Use 'json' or 'glb'.");
}
}
[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}");
}
}
}
\ No newline at end of file
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Bson;
namespace MMM_Server.Models;
public class Account
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string? AccountID { get; set; }
public string Header { get; set; } = null!;
public string MInstanceID { get; set; } = null!;
public string MEnvironmentID { get; set; } = null!;
public string HumanID { get; set; } = null!;
public string PersonalProfileID { get; set; } = null!;
[BsonElement("Rights")]
public List<Right> Rights { get; set; } = null;
[BsonElement("Users")]
public List<User> Users { get; set; } = null;
[BsonElement("Personae")]
public List<Persona> Personae { get; set; } = null!;
public string DescrMetadata { get; set; } = null!;
}
\ No newline at end of file
......@@ -11,6 +11,7 @@
public string UsersCollectionName { get; set; } = null!;
public string PersonaeCollectionName { get; set; } = null!;
public string DevicesCollectionName { get; set; } = null!;
public string AccountsCollectionName { get; set; } = null!;
}
......
......@@ -7,7 +7,7 @@ namespace MMM_Server.Models
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string? Id { get; set; }
public string? PersonaID { get; set; }
public string Model { get; set; } = null!;
}
......
......@@ -7,15 +7,13 @@ namespace MMM_Server.Models
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string? Id { get; set; }
public string? PersonalProfileID { get; set; }
public string Header { get; set; } = null!;
public string MInstanceID { get; set; } = null!;
public string HumanID { get; set; } = null!;
public string PersonalProfileID { get; set; } = null!;
public string HumanID { get; set; } = null!;
public PersonalProfileData PersonalProfileData { get; set; } = null!;
......
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Bson;
namespace MMM_Server.Models;
public class Right
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string? RightID { get; set; }
public string Header { get; set; } = null!;
public string MInstanceID { get; set; } = null!;
[BsonElement("RightsData")]
public List<RightData> RightsData { get; set; } = null!;
public string DescrMetadata { get; set; } = null!;
}
using MongoDB.Bson;
namespace MMM_Server.Models;
public class RightData
{
//TODO: complete definition of RightData
public string Level{ get; set; } = null!;
public string ProcessAction { get; set; } = null!;
}
......@@ -7,8 +7,6 @@ namespace MMM_Server.Models
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string? Id { get; set; }
public string? Id { get; set; }
}
}
......@@ -9,6 +9,7 @@ builder.Services.AddSingleton<PersonalProfileService>();
builder.Services.AddSingleton<UserService>();
builder.Services.AddSingleton<DeviceService>();
builder.Services.AddSingleton<PersonaService>();
builder.Services.AddSingleton<AccountService>();
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
......
{"BodyType":"fullbody","OutfitGender":2,"UpdatedAt":"2024-11-27T18:09:16.035Z","SkinTone":"#bd745a"}
\ No newline at end of file
{"BodyType":"fullbody","OutfitGender":1,"UpdatedAt":"2024-11-27T16:29:06.037Z","SkinTone":"#b7785e"}
\ No newline at end of file
using Microsoft.Extensions.Options;
using MMM_Server.Models;
using MongoDB.Bson;
using MongoDB.Driver;
namespace MMM_Server.Services;
public class AccountService : MongoDbService<Account>
{
public AccountService(IOptions<MMMDatabaseSettings> accountDatabaseSettings)
: base(accountDatabaseSettings, accountDatabaseSettings.Value.AccountsCollectionName)
{
}
// You can either override the base class GetAsync method (if you need to customize behavior)
public async Task<Account?> GetAsync(string id)
{
// Add custom logic if needed
return await _collection.Find(x => x.AccountID == id).FirstOrDefaultAsync();
}
public async Task UpdateAsync(string id, Persona updatedItem)
{
// Filter to find the account by its AccountID
var filter = Builders<Account>.Filter.Eq(account => account.AccountID, id);
// Find the account
var account = await _collection.Find(filter).FirstOrDefaultAsync();
if (account == null)
{
throw new Exception($"Account with ID {id} not found.");
}
// Check if the persona already exists in the Personae array
if (updatedItem.PersonaID == null)
{
updatedItem.PersonaID = ObjectId.GenerateNewId().ToString();
}
var existingPersona = account.Personae.FirstOrDefault(p => p.PersonaID == updatedItem.PersonaID);
if (existingPersona != null)
{
// Update the existing persona
existingPersona.Model = updatedItem.Model;
}
else
{
// Add the new persona
account.Personae.Add(updatedItem);
}
// Update the Personae array in the database
var update = Builders<Account>.Update.Set(account => account.Personae, account.Personae);
var result = await _collection.UpdateOneAsync(filter, update);
// Throw exception if the update fails
if (result.MatchedCount == 0)
{
throw new Exception($"Failed to update the account with ID {id}.");
}
}
}
......@@ -6,7 +6,7 @@ namespace MMM_Server.Services;
public class MongoDbService<T>
{
private readonly IMongoCollection<T> _collection;
protected readonly IMongoCollection<T> _collection;
public MongoDbService(
IOptions<MMMDatabaseSettings> databaseSettings,
......@@ -23,12 +23,15 @@ public class MongoDbService<T>
public async Task CreateAsync(T newItem) =>
await _collection.InsertOneAsync(newItem);
//public async Task<T?> GetAsync(string id) =>
// await _collection.Find(x => x.Id == id).FirstOrDefaultAsync();
//TODO: complete following defintions
//public async Task UpdateAsync(string id, T updatedItem) =>
// await _collection.ReplaceOneAsync(x => x.Id == id, updatedItem);
//public async Task RemoveAsync(string id) =>
// await _collection.DeleteOneAsync(x => x.Id == id);
//public async Task<T?> GetAsync(string id) =>
// await _collection.Find(x => x.Id == id).FirstOrDefaultAsync();
}
......@@ -5,7 +5,9 @@
"ProfilesCollectionName": "PersonalProfiles",
"UsersCollectionName": "Users",
"DevicesCollectionName": "Devices",
"PersonaeCollectionName": "Personae"
"PersonaeCollectionName": "Personae",
"AccountsCollectionName": "Accounts"
},
"Logging": {
"LogLevel": {
......
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