Files
moadiran/Back/Services/ServValidatinMsg.cs
2024-04-16 22:56:02 +03:30

53 lines
1.8 KiB
C#

using Back.Common.Enums;
using Back.Data.Contracts;
using Back.Data.Models;
using Microsoft.EntityFrameworkCore;
namespace Back.Services
{
public class ServValidatinMsg
{
private readonly IAsyncRepository<VerificationCode> _verificationCodeRepo;
private readonly IAsyncRepository<Ticket> _ticket;
public ServValidatinMsg(IAsyncRepository<VerificationCode> verificationCodeRepo, IAsyncRepository<Ticket> ticket)
{
_verificationCodeRepo = verificationCodeRepo;
_ticket = ticket;
}
public async Task<VerificationCode> GetCodeByPrm(string Prm)
{
return await _verificationCodeRepo.Get(w => w.prm == Prm).FirstOrDefaultAsync();
}
public async Task<VerificationCode> GetVerificationCode(int ID)
{
return await _verificationCodeRepo.Get(w => w.ID == ID).FirstOrDefaultAsync();
}
public async Task<bool> Delete(VerificationCode model)
{
return await _verificationCodeRepo.DeleteAsync(model);
}
public async Task<bool> SubmittedTicket(VerificationCode code)
{
var ticket = await _ticket.Get(w => w.ID == Convert.ToInt32(code.prm)).FirstOrDefaultAsync();
if (ticket != null && ticket.Status== StatusTicket.unknownPerson)
{
ticket.Status = StatusTicket.Awaitingreview;
return await _ticket.UpdateAsync(ticket);
}
return false;
}
public async Task<int> GenerateCode(VerificationCode code)
{
code.Code = Random.Shared.Next(1000, 9000);
while (await GetVerificationCode(code.ID) != null)
code.Code = Random.Shared.Next(1000, 9000);
var indb= await _verificationCodeRepo.AddAsync(code);
return indb.ID;
}
}
}