using Microsoft.AspNetCore.Http.HttpResults; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Caching.Memory; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using MTWireGuard.Application.Repositories; using Microsoft.AspNetCore.Mvc; using MTWireGuard.Application.Models.Mikrotik; using AutoMapper; using MTWireGuard.Application.Models.Requests; using MTWireGuard.Application.Models.Models.Responses; using MTWireGuard.Application.Models; namespace MTWireGuard.Application.MinimalAPI { internal class UserController { public static async Task>, NotFound>> GetAll([FromServices] IMikrotikRepository API, HttpContext context) { var script = await API.RunScript("SendActivityUpdates"); var users = await API.GetUsersAsync(); if (users.Count > 0) return TypedResults.Ok(users); return TypedResults.NotFound(); } public static async Task, NotFound>> GetById([FromServices] IMikrotikRepository API, int id) { var user = await API.GetUser(id); if (user != null) return TypedResults.Ok(user); return TypedResults.NotFound(); } public static async Task> GetQR([FromServices] IMikrotikRepository API, int id) { string config = await API.GetQRCodeBase64(id); return TypedResults.Ok(config); } public static async Task GetFile([FromServices] IMikrotikRepository API, int id) { string config = await API.GetUserTunnelConfig(id); byte[] bytesInStream = Encoding.UTF8.GetBytes(config); var user = await API.GetUser(id); string filename = string.IsNullOrWhiteSpace(user.Name) ? user.Interface : user.Name; return TypedResults.File( fileContents: bytesInStream, fileDownloadName: filename); } public static async Task> Create( [FromServices] IMikrotikRepository API, [FromServices] IMapper mapper, [FromBody] CreateClientRequest request) //HttpRequest request) { //var ucm = await request.ReadFromJsonAsync(); var model = mapper.Map(request); var make = await API.CreateUser(model); var message = mapper.Map(make); return TypedResults.Ok(message); } public static async Task> Update( [FromServices] IMikrotikRepository API, [FromServices] IMapper mapper, int id, UpdateClientRequest request) { request.ID = id; var model = mapper.Map(request); var update = await API.UpdateUser(model); var message = mapper.Map(update); return TypedResults.Ok(message); } public static async Task> Sync( [FromServices] IMikrotikRepository API, [FromServices] IMapper mapper, int id, SyncUserRequest request) { request.ID = id; var model = mapper.Map(request); var update = await API.SyncUser(model); var message = mapper.Map(update); return TypedResults.Ok(message); } public static async Task> Delete( [FromServices] IMikrotikRepository API, [FromServices] IMapper mapper, [FromRoute] int id) { var delete = await API.DeleteUser(id); var message = mapper.Map(delete); return TypedResults.Ok(message); } public static async Task> Activation( [FromServices] IMikrotikRepository API, [FromServices] IMapper mapper, [FromRoute] int id, ChangeStateRequest request) { request.Id = id; var active = (!request.Enabled) ? await API.EnableUser(request.Id) : await API.DisableUser(request.Id); var message = mapper.Map(active); return TypedResults.Ok(message); } } }