120 lines
4.5 KiB
C#
120 lines
4.5 KiB
C#
namespace qtc_api.Services.ContactService
|
|
{
|
|
public class ContactService : IContactService
|
|
{
|
|
private readonly DataContext _dataContext;
|
|
|
|
public ContactService(DataContext dataContext)
|
|
{
|
|
_dataContext = dataContext;
|
|
}
|
|
|
|
public async Task<ServiceResponse<Contact>> CreateContact(string ownerId, string userId)
|
|
{
|
|
var serviceResponse = new ServiceResponse<Contact>();
|
|
var rnd = LongRandom(1, 900000000000000000, new Random());
|
|
|
|
var contact = new Contact()
|
|
{
|
|
Id = rnd.ToString(),
|
|
OwnerId = ownerId,
|
|
UserId = userId,
|
|
OwnerStatus = Contact.ContactStatus.AwaitingApprovalFromOther,
|
|
UserStatus = Contact.ContactStatus.AwaitingApprovalFromSelf,
|
|
};
|
|
|
|
await _dataContext.Contacts.AddAsync(contact);
|
|
await _dataContext.SaveChangesAsync();
|
|
|
|
serviceResponse.Success = true;
|
|
serviceResponse.Data = contact;
|
|
|
|
return serviceResponse;
|
|
}
|
|
|
|
public async Task<ServiceResponse<bool>> UpdateContactStatus(string ownerId, string userId, Contact.ContactStatus ownerStatus, Contact.ContactStatus userStatus)
|
|
{
|
|
var serviceResponse = new ServiceResponse<bool>();
|
|
|
|
var contact = await _dataContext.Contacts.FirstOrDefaultAsync(e => (e.OwnerId == ownerId && e.UserId == userId) || (e.OwnerId == userId && e.UserId == ownerId));
|
|
|
|
if (contact != null)
|
|
{
|
|
contact.OwnerStatus = ownerStatus;
|
|
contact.UserStatus = userStatus;
|
|
serviceResponse.Success = true;
|
|
}
|
|
else serviceResponse.Success = false;
|
|
|
|
await _dataContext.SaveChangesAsync();
|
|
|
|
return serviceResponse;
|
|
}
|
|
|
|
public async Task<ServiceResponse<Contact>> DeleteContact(string ownerId, string userId)
|
|
{
|
|
var serviceResponse = new ServiceResponse<Contact>();
|
|
var contact = await _dataContext.Contacts.FirstOrDefaultAsync(e => (e.OwnerId == ownerId && e.UserId == userId) || (e.OwnerId == userId && e.UserId == ownerId));
|
|
|
|
if (contact != null)
|
|
{
|
|
var result = _dataContext.Contacts.Remove(contact);
|
|
await _dataContext.SaveChangesAsync();
|
|
|
|
serviceResponse.Success = true;
|
|
serviceResponse.Data = result.Entity;
|
|
|
|
return serviceResponse;
|
|
}
|
|
else
|
|
{
|
|
serviceResponse.Success = false;
|
|
serviceResponse.Data = null;
|
|
serviceResponse.Message = "Contact Not Found";
|
|
|
|
return serviceResponse;
|
|
}
|
|
}
|
|
|
|
public async Task<ServiceResponse<Contact>> GetContactById(string id)
|
|
{
|
|
var serviceResponse = new ServiceResponse<Contact>();
|
|
|
|
var contact = await _dataContext.Contacts.FirstOrDefaultAsync(x => x.Id == id);
|
|
|
|
if (contact == null) { serviceResponse.Success = false; serviceResponse.Message = "Contact Does Not Exist."; }
|
|
else { serviceResponse.Success = true; serviceResponse.Data = contact; }
|
|
|
|
return serviceResponse;
|
|
}
|
|
|
|
public async Task<ServiceResponse<List<Contact>>> GetUserContacts(User user)
|
|
{
|
|
var serviceResponse = new ServiceResponse<List<Contact>>();
|
|
|
|
var contactsMade = await _dataContext.Contacts.Where(x => x.OwnerId == user.Id).ToListAsync();
|
|
var contactsList = await _dataContext.Contacts.Where(x => x.UserId == user.Id).ToListAsync();
|
|
|
|
var contactsCombined = new List<Contact>();
|
|
|
|
foreach (var contact in contactsMade)
|
|
contactsCombined.Add(contact); // all contacts the user has made
|
|
foreach (var contact in contactsList)
|
|
contactsCombined.Add(contact); // all contacts the user has received
|
|
|
|
if (contactsCombined.Count == 0) { serviceResponse.Success = true; serviceResponse.Message = "User Has No Contacts."; }
|
|
else { serviceResponse.Success = true; serviceResponse.Data = contactsCombined; }
|
|
|
|
return serviceResponse;
|
|
}
|
|
|
|
private long LongRandom(long min, long max, Random rnd)
|
|
{
|
|
long result = rnd.Next((int)(min >> 32), (int)(max >> 32));
|
|
result = result << 32;
|
|
result = result | (long)rnd.Next((int)min, (int)max);
|
|
return result;
|
|
}
|
|
}
|
|
}
|