122 lines
4.5 KiB
C#
122 lines
4.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using _0_Framework.Application;
|
|
using AccountManagement.Application.Contracts.Account;
|
|
using AccountManagement.Application.Contracts.CameraAccount;
|
|
using AccountManagement.Domain.AccountAgg;
|
|
using AccountManagement.Domain.CameraAccountAgg;
|
|
|
|
namespace AccountManagement.Application;
|
|
|
|
public class CameraAccountApplication : ICameraAccountApplication
|
|
{
|
|
private readonly ICameraAccountRepository _cameraAccountRepository;
|
|
private readonly IPasswordHasher _passwordHasher;
|
|
private readonly IAccountRepository _accountRepository;
|
|
|
|
public CameraAccountApplication(ICameraAccountRepository cameraAccountRepository, IPasswordHasher passwordHasher, IAccountRepository accountRepository)
|
|
{
|
|
_cameraAccountRepository = cameraAccountRepository;
|
|
_passwordHasher = passwordHasher;
|
|
_accountRepository = accountRepository;
|
|
}
|
|
|
|
public OperationResult Create(CreateCameraAccount command)
|
|
{
|
|
var opreation = new OperationResult();
|
|
if (string.IsNullOrWhiteSpace(command.Username) || string.IsNullOrWhiteSpace(command.Password))
|
|
return opreation.Failed("وارد کردن نام کاربری و گذرواژه الزامیست");
|
|
if (_cameraAccountRepository.Exists(x => x.Username == command.Username))
|
|
return opreation.Failed("نام کاربری تکراری است");
|
|
if (command.Password.Length < 8)
|
|
return opreation.Failed("گذرواژه نباید کمتر از 8 کاراکتر باشد");
|
|
if (command.Username.Length < 6)
|
|
return opreation.Failed("نام کاربری نباید کمتر از 6 کاراکتر باشد");
|
|
var password = _passwordHasher.Hash(command.Password);
|
|
var create = new CameraAccount(command.Username, password, command.Mobile, command.WorkshopName,
|
|
command.WorkshopId, command.AccountId);
|
|
_cameraAccountRepository.Create(create);
|
|
_cameraAccountRepository.SaveChanges();
|
|
return opreation.Succcedded();
|
|
|
|
}
|
|
|
|
public OperationResult Edit(EditCameraAccount command)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public EditCameraAccount GetDetails(long id)
|
|
{
|
|
return _cameraAccountRepository.GetDetails(id);
|
|
}
|
|
|
|
public bool HasCameraAccount(long workshopId, long accountId)
|
|
{
|
|
return _cameraAccountRepository.Exists(x => x.WorkshopId == workshopId && x.AccountId == accountId);
|
|
}
|
|
|
|
public OperationResult CheckUsername(string username)
|
|
{
|
|
var operation = new OperationResult();
|
|
if (_cameraAccountRepository.Exists(x => x.Username == username) || _accountRepository.Exists(x => x.Username == username))
|
|
return operation.Failed("نام کاربری تکراری است");
|
|
return operation.Succcedded();
|
|
}
|
|
|
|
public OperationResult Active(long id)
|
|
{
|
|
var opration = new OperationResult();
|
|
var acc = _cameraAccountRepository.Get(id);
|
|
if (acc == null)
|
|
return opration.Failed("رکورد مورد نظر یافت نشد");
|
|
|
|
acc.Active();
|
|
|
|
_cameraAccountRepository.SaveChanges();
|
|
return opration.Succcedded();
|
|
}
|
|
|
|
public OperationResult DeActive(long id)
|
|
{
|
|
var opration = new OperationResult();
|
|
var acc = _cameraAccountRepository.Get(id);
|
|
if (acc == null)
|
|
return opration.Failed("رکورد مورد نظر یافت نشد");
|
|
|
|
acc.DeActive();
|
|
|
|
_cameraAccountRepository.SaveChanges();
|
|
return opration.Succcedded();
|
|
}
|
|
|
|
public OperationResult ChangePass(ChangePassword command)
|
|
{
|
|
var operation = new OperationResult();
|
|
var account = _cameraAccountRepository.Get(command.Id);
|
|
if (account == null)
|
|
return operation.Failed(ApplicationMessages.RecordNotFound);
|
|
|
|
if (command.Password != command.RePassword)
|
|
return operation.Failed(ApplicationMessages.PasswordsNotMatch);
|
|
if (command.Password.Length < 8)
|
|
return operation.Failed("تعداد کاراکترهای گذرواژه نباید کمتر از 8 باشد");
|
|
|
|
var password = _passwordHasher.Hash(command.Password);
|
|
account.ChangePassword(password);
|
|
_cameraAccountRepository.SaveChanges();
|
|
return operation.Succcedded();
|
|
}
|
|
|
|
#region Safa
|
|
|
|
public EditCameraAccount GetDetailsByWorkshop(long workshopId)
|
|
{
|
|
return _cameraAccountRepository.GetDetailsByWorkshop(workshopId);
|
|
}
|
|
|
|
#endregion
|
|
} |