Spaces:
Running
Running
File size: 1,247 Bytes
47e77e5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | using Microsoft.EntityFrameworkCore;
using TaskTrackingSystem.Database.AppDbContextModels;
using TaskTrackingSystem.Shared;
using DbUserDevice = TaskTrackingSystem.Database.AppDbContextModels.UserDevice;
namespace TaskTrackingSystem.WebApi.Features.UserDevice;
public class UserDeviceService
{
private readonly AppDbContext _db;
public UserDeviceService(AppDbContext db)
{
_db = db;
}
public async Task<Result> RegisterTokenAsync(long userId, string fcmToken)
{
var token = fcmToken.Trim();
if (string.IsNullOrWhiteSpace(token))
{
return Result.Failure("FCM token is required.", 400);
}
var existing = await _db.UserDevices.FirstOrDefaultAsync(x => x.FcmToken == token);
var now = DateTime.UtcNow;
if (existing == null)
{
_db.UserDevices.Add(new DbUserDevice
{
UserId = userId,
FcmToken = token,
CreatedAt = now,
UpdatedAt = now
});
}
else
{
existing.UserId = userId;
existing.UpdatedAt = now;
}
await _db.SaveChangesAsync();
return Result.Success();
}
}
|