This repository has been archived by the owner on Aug 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DeviceModel, DeviceManufacturer, and DeviceSpecification + Cleanup
- Loading branch information
Showing
18 changed files
with
723 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using ZapMe.Enums.Devices; | ||
|
||
namespace ZapMe.DTOs; | ||
|
||
public readonly struct DeviceManufacturerDto | ||
{ | ||
public Guid Id { get; init; } | ||
|
||
public string Name { get; init; } | ||
|
||
public string ModelWebsiteUrl { get; init; } | ||
|
||
public Uri IconUrl { get; init; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using ZapMe.Enums.Devices; | ||
|
||
namespace ZapMe.DTOs; | ||
|
||
public readonly struct DeviceModelDto | ||
{ | ||
public Guid Id { get; init; } | ||
|
||
public string Name { get; init; } | ||
|
||
public Guid ModelId { get; init; } | ||
|
||
public string ModelName { get; init; } | ||
|
||
public string ModelNumber { get; init; } | ||
|
||
public string ModelWebsiteUrl { get; init; } | ||
|
||
public Uri IconUrl { get; init; } | ||
|
||
public RfProtocol Protocol { get; init; } | ||
|
||
public Guid ManufacturerId { get; init; } | ||
|
||
public string ManufacturerName { get; init; } | ||
|
||
public string ManufacturerWebsiteUrl { get; init; } | ||
|
||
public DateTime RegisteredAt { get; init; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using ZapMe.Database.Documents; | ||
|
||
namespace ZapMe.DTOs; | ||
|
||
public readonly struct DeviceSpecificationDto | ||
{ | ||
public Guid Id { get; init; } | ||
|
||
public ulong Frequency { get; init; } | ||
|
||
public DeviceProprietarySpecification.ModulationType Modulation { get; init; } | ||
|
||
public ulong DataRate { get; init; } | ||
|
||
public ulong PacketSize { get; init; } | ||
|
||
public DeviceProprietarySpecification.EncodingType Encoding { get; init; } | ||
|
||
public bool InvertedBits { get; init; } | ||
} |
3 changes: 1 addition & 2 deletions
3
Common/Database/Migrations/20230707120742_Add-Device-And-ControlGrant.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
Common/Database/Migrations/20230709122559_Remove-Duplicate-Device-Table.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
using Microsoft.OpenApi.Any; | ||
using Microsoft.OpenApi.Models; | ||
using System.ComponentModel.DataAnnotations; | ||
using ZapMe.BusinessRules; | ||
|
||
namespace ZapMe.Attributes; | ||
|
||
/// <summary> | ||
/// An attribute used to validate whether a display name is valid. | ||
/// </summary> | ||
/// <remarks> | ||
/// Inherits from <see cref="ValidationAttribute"/>. | ||
/// </remarks> | ||
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)] | ||
public class DisplaynameAttribute : ValidationAttribute, IParameterAttribute | ||
{ | ||
/// <summary> | ||
/// Regular expression for username validation. | ||
/// </summary> | ||
public const string DisplaynameRegex = /* lang=regex */ @"^[^\s].*[^\s]$"; | ||
|
||
/// <summary> | ||
/// Example username used to generate OpenApi documentation. | ||
/// </summary> | ||
public const string ExampleDisplayname = "String"; | ||
|
||
private const string _ErrMsgMustBeString = "Must be a string"; | ||
|
||
/// <summary> | ||
/// Indicates whether validation should be performed. | ||
/// </summary> | ||
public bool ShouldValidate { get; } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="DisplaynameAttribute"/> class with the specified validation behavior. | ||
/// </summary> | ||
/// <param name="shouldValidate">True if validation should be performed; otherwise, false.</param> | ||
public DisplaynameAttribute(bool shouldValidate) | ||
{ | ||
ShouldValidate = shouldValidate; | ||
} | ||
|
||
/// <inheritdoc/> | ||
protected override ValidationResult? IsValid(object? value, ValidationContext validationContext) | ||
{ | ||
if (!ShouldValidate) return ValidationResult.Success; | ||
|
||
if (value is null) | ||
{ | ||
return ValidationResult.Success; | ||
} | ||
|
||
if (value is not string displayname) | ||
{ | ||
return new ValidationResult(_ErrMsgMustBeString); | ||
} | ||
|
||
if (Char.IsWhiteSpace(displayname[0]) || Char.IsWhiteSpace(displayname[^1])) | ||
{ | ||
return new ValidationResult("Displayname cannot start or end with whitespace."); | ||
} | ||
|
||
if (UIStringValidator.IsBadUiString(displayname)) | ||
{ | ||
return new ValidationResult("Displayname must not contain obnoxious characters."); | ||
} | ||
|
||
return ValidationResult.Success; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public void Apply(OpenApiSchema schema) | ||
{ | ||
if (ShouldValidate) | ||
{ | ||
schema.Pattern = DisplaynameRegex; | ||
} | ||
|
||
schema.Example = new OpenApiString(ExampleDisplayname); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public void Apply(OpenApiParameter parameter) | ||
{ | ||
Apply(parameter.Schema); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.EntityFrameworkCore; | ||
using System.Security.Claims; | ||
using ZapMe.Attributes; | ||
using ZapMe.Database.Models; | ||
using ZapMe.DTOs; | ||
using ZapMe.Utils; | ||
|
||
namespace ZapMe.Controllers.Api.V1; | ||
|
||
public partial class DeviceController | ||
{ | ||
public readonly record struct RegisterDeviceRequest | ||
{ | ||
public Guid DeviceModelId { get; init; } | ||
|
||
[Displayname(true)] | ||
public string Name { get; init; } | ||
} | ||
|
||
/// <summary> | ||
/// Register a device. | ||
/// </summary> | ||
/// <returns></returns> | ||
/// <response code="200">Device</response> | ||
[HttpPost(Name = "RegisterDevice")] | ||
[ProducesResponseType(typeof(DeviceDto), StatusCodes.Status200OK)] | ||
[ProducesResponseType(StatusCodes.Status401Unauthorized)] | ||
public async Task<IActionResult> Register(RegisterDeviceRequest data, CancellationToken cancellationToken) | ||
{ | ||
var deviceModel = await _dbContext.DeviceModels | ||
.AsNoTracking() | ||
.Where(dm => dm.Id == data.DeviceModelId) | ||
.FirstOrDefaultAsync(cancellationToken); | ||
|
||
if (deviceModel is null) | ||
return NotFound(); | ||
|
||
var device = new DeviceEntity | ||
{ | ||
ModelId = deviceModel.Id, | ||
OwnerId = User.GetUserId(), | ||
Name = data.Name, | ||
AccessToken = StringUtils.GenerateUrlSafeRandomString(64) | ||
}; | ||
|
||
_dbContext.Devices.Add(device); | ||
await _dbContext.SaveChangesAsync(cancellationToken); | ||
|
||
return Ok(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using ZapMe.Attributes; | ||
using ZapMe.Database.Models; | ||
using ZapMe.DTOs; | ||
|
||
namespace ZapMe.Controllers.Api.V1; | ||
|
||
public partial class DeviceController | ||
{ | ||
public readonly struct RegisterDeviceManufacturerRequest | ||
{ | ||
[Displayname(true)] | ||
public required string Name { get; init; } | ||
|
||
public required string WebsiteUrl { get; init; } | ||
|
||
public Guid IconId { get; init; } | ||
} | ||
|
||
/// <summary> | ||
/// Register a device manufacturer. | ||
/// </summary> | ||
/// <returns></returns> | ||
/// <response code="200">Device</response> | ||
[HttpPost("manufacturer", Name = "RegisterDeviceManufacturer")] | ||
[ProducesResponseType(typeof(DeviceManufacturerDto), StatusCodes.Status200OK)] | ||
[ProducesResponseType(StatusCodes.Status401Unauthorized)] | ||
public async Task<IActionResult> RegisterManufacturer(RegisterDeviceManufacturerRequest data, CancellationToken cancellationToken) | ||
{ | ||
var deviceManufacturer = new DeviceManufacturerEntity | ||
{ | ||
Name = data.Name, | ||
WebsiteUrl = data.WebsiteUrl, | ||
IconId = data.IconId, | ||
}; | ||
|
||
_dbContext.DeviceManufacturers.Add(deviceManufacturer); | ||
await _dbContext.SaveChangesAsync(cancellationToken); | ||
|
||
return Ok(); | ||
} | ||
} |
Oops, something went wrong.