|
| 1 | +using LabSync.Core.Dto; |
| 2 | +using LabSync.Core.Entities; |
| 3 | +using LabSync.Server.Data; |
| 4 | +using Microsoft.AspNetCore.Authorization; |
| 5 | +using Microsoft.AspNetCore.Mvc; |
| 6 | +using Microsoft.EntityFrameworkCore; |
| 7 | + |
| 8 | +namespace LabSync.Server.Controllers; |
| 9 | + |
| 10 | +[ApiController] |
| 11 | +[Route("api/device-groups")] |
| 12 | +[Authorize(Policy = "RequireAdminRole")] |
| 13 | +public sealed class DeviceGroupsController(LabSyncDbContext context) : ControllerBase |
| 14 | +{ |
| 15 | + private const int MaxNameLength = 100; |
| 16 | + private const int MaxDescriptionLength = 500; |
| 17 | + |
| 18 | + [HttpGet] |
| 19 | + public async Task<ActionResult<IEnumerable<DeviceGroupDto>>> GetAll(CancellationToken cancellationToken) |
| 20 | + { |
| 21 | + var groups = await context.Set<DeviceGroup>() |
| 22 | + .AsNoTracking() |
| 23 | + .Include(g => g.Devices) |
| 24 | + .OrderBy(g => g.Name) |
| 25 | + .ToListAsync(cancellationToken); |
| 26 | + |
| 27 | + return Ok(groups.Select(ToDto)); |
| 28 | + } |
| 29 | + |
| 30 | + [HttpPost] |
| 31 | + public async Task<ActionResult<DeviceGroupDto>> Create( |
| 32 | + [FromBody] CreateDeviceGroupRequest request, |
| 33 | + CancellationToken cancellationToken) |
| 34 | + { |
| 35 | + var validation = Validate(request.Name, request.Description); |
| 36 | + if (validation is not null) |
| 37 | + return BadRequest(new ApiResponse(validation)); |
| 38 | + |
| 39 | + var normalizedName = request.Name.Trim(); |
| 40 | + var exists = await context.Set<DeviceGroup>() |
| 41 | + .AnyAsync(g => g.Name.ToLower() == normalizedName.ToLower(), cancellationToken); |
| 42 | + if (exists) |
| 43 | + return BadRequest(new ApiResponse("A group with this name already exists.")); |
| 44 | + |
| 45 | + var group = new DeviceGroup(normalizedName, request.Description?.Trim()); |
| 46 | + context.Set<DeviceGroup>().Add(group); |
| 47 | + await context.SaveChangesAsync(cancellationToken); |
| 48 | + |
| 49 | + return CreatedAtAction(nameof(GetAll), new { id = group.Id }, ToDto(group)); |
| 50 | + } |
| 51 | + |
| 52 | + [HttpPut("{id:guid}")] |
| 53 | + public async Task<ActionResult<DeviceGroupDto>> Update( |
| 54 | + Guid id, |
| 55 | + [FromBody] UpdateDeviceGroupRequest request, |
| 56 | + CancellationToken cancellationToken) |
| 57 | + { |
| 58 | + var validation = Validate(request.Name, request.Description); |
| 59 | + if (validation is not null) |
| 60 | + return BadRequest(new ApiResponse(validation)); |
| 61 | + |
| 62 | + var group = await context.Set<DeviceGroup>() |
| 63 | + .Include(g => g.Devices) |
| 64 | + .FirstOrDefaultAsync(g => g.Id == id, cancellationToken); |
| 65 | + if (group is null) |
| 66 | + return NotFound(new ApiResponse("Group not found.")); |
| 67 | + |
| 68 | + var normalizedName = request.Name.Trim(); |
| 69 | + var duplicate = await context.Set<DeviceGroup>() |
| 70 | + .AnyAsync(g => g.Id != id && g.Name.ToLower() == normalizedName.ToLower(), cancellationToken); |
| 71 | + if (duplicate) |
| 72 | + return BadRequest(new ApiResponse("A group with this name already exists.")); |
| 73 | + |
| 74 | + group.UpdateDetails(normalizedName, request.Description?.Trim()); |
| 75 | + await context.SaveChangesAsync(cancellationToken); |
| 76 | + |
| 77 | + return Ok(ToDto(group)); |
| 78 | + } |
| 79 | + |
| 80 | + [HttpDelete("{id:guid}")] |
| 81 | + public async Task<ActionResult> Delete(Guid id, CancellationToken cancellationToken) |
| 82 | + { |
| 83 | + var group = await context.Set<DeviceGroup>().FirstOrDefaultAsync(g => g.Id == id, cancellationToken); |
| 84 | + if (group is null) |
| 85 | + return NotFound(new ApiResponse("Group not found.")); |
| 86 | + |
| 87 | + context.Set<DeviceGroup>().Remove(group); |
| 88 | + await context.SaveChangesAsync(cancellationToken); |
| 89 | + return NoContent(); |
| 90 | + } |
| 91 | + |
| 92 | + private static string? Validate(string name, string? description) |
| 93 | + { |
| 94 | + if (string.IsNullOrWhiteSpace(name)) |
| 95 | + return "Group name is required."; |
| 96 | + if (name.Trim().Length > MaxNameLength) |
| 97 | + return $"Group name cannot exceed {MaxNameLength} characters."; |
| 98 | + if (!string.IsNullOrWhiteSpace(description) && description.Trim().Length > MaxDescriptionLength) |
| 99 | + return $"Description cannot exceed {MaxDescriptionLength} characters."; |
| 100 | + return null; |
| 101 | + } |
| 102 | + |
| 103 | + private static DeviceGroupDto ToDto(DeviceGroup group) |
| 104 | + { |
| 105 | + var devices = group.Devices.Select(d => new DeviceGroupDeviceDto |
| 106 | + { |
| 107 | + Id = d.Id, |
| 108 | + Hostname = d.Hostname, |
| 109 | + IsOnline = d.IsOnline |
| 110 | + }).ToArray(); |
| 111 | + |
| 112 | + return new DeviceGroupDto |
| 113 | + { |
| 114 | + Id = group.Id, |
| 115 | + Name = group.Name, |
| 116 | + Description = group.Description, |
| 117 | + CreatedAt = group.CreatedAt, |
| 118 | + DeviceCount = devices.Length, |
| 119 | + Devices = devices |
| 120 | + }; |
| 121 | + } |
| 122 | +} |
0 commit comments