bitwarden-android/src/App/Services/LoginService.cs

108 lines
3.4 KiB
C#
Raw Normal View History

2016-05-02 09:52:09 +03:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Bit.App.Abstractions;
using Bit.App.Models;
2016-05-03 09:08:50 +03:00
using Bit.App.Models.Api;
2016-05-02 09:52:09 +03:00
using Bit.App.Models.Data;
using Xamarin.Forms;
2016-05-02 09:52:09 +03:00
namespace Bit.App.Services
{
2017-01-03 08:17:15 +03:00
public class LoginService : ILoginService
2016-05-02 09:52:09 +03:00
{
2017-01-03 08:17:15 +03:00
private readonly ILoginRepository _loginRepository;
2016-05-03 00:50:16 +03:00
private readonly IAuthService _authService;
2017-01-03 08:17:15 +03:00
private readonly ILoginApiRepository _loginApiRepository;
2016-05-02 09:52:09 +03:00
2017-01-03 08:17:15 +03:00
public LoginService(
ILoginRepository loginRepository,
IAuthService authService,
2017-01-03 08:17:15 +03:00
ILoginApiRepository loginApiRepository)
2016-05-02 09:52:09 +03:00
{
2017-01-03 08:17:15 +03:00
_loginRepository = loginRepository;
2016-05-03 00:50:16 +03:00
_authService = authService;
2017-01-03 08:17:15 +03:00
_loginApiRepository = loginApiRepository;
2016-05-03 00:50:16 +03:00
}
2017-01-03 08:17:15 +03:00
public async Task<Login> GetByIdAsync(string id)
2016-05-03 00:50:16 +03:00
{
2017-01-03 08:17:15 +03:00
var data = await _loginRepository.GetByIdAsync(id);
if(data == null || data.UserId != _authService.UserId)
{
return null;
}
2017-01-03 08:17:15 +03:00
var login = new Login(data);
return login;
}
2017-01-03 08:17:15 +03:00
public async Task<IEnumerable<Login>> GetAllAsync()
{
2017-01-03 08:17:15 +03:00
var data = await _loginRepository.GetAllByUserIdAsync(_authService.UserId);
var logins = data.Select(f => new Login(f));
return logins;
2016-05-02 09:52:09 +03:00
}
2017-01-03 08:17:15 +03:00
public async Task<IEnumerable<Login>> GetAllAsync(bool favorites)
{
2017-01-03 08:17:15 +03:00
var data = await _loginRepository.GetAllByUserIdAsync(_authService.UserId, favorites);
var logins = data.Select(f => new Login(f));
return logins;
}
2017-01-03 08:17:15 +03:00
public async Task<ApiResult<LoginResponse>> SaveAsync(Login login)
2016-05-02 09:52:09 +03:00
{
2017-01-03 08:17:15 +03:00
ApiResult<LoginResponse> response = null;
var request = new LoginRequest(login);
2016-05-03 09:08:50 +03:00
2017-01-03 08:17:15 +03:00
if(login.Id == null)
2016-05-02 09:52:09 +03:00
{
2017-01-03 08:17:15 +03:00
response = await _loginApiRepository.PostAsync(request);
2016-05-02 09:52:09 +03:00
}
else
{
2017-01-03 08:17:15 +03:00
response = await _loginApiRepository.PutAsync(login.Id, request);
}
if(response.Succeeded)
{
2017-01-03 08:17:15 +03:00
var data = new LoginData(response.Result, _authService.UserId);
if(login.Id == null)
{
2017-01-03 08:17:15 +03:00
await _loginRepository.InsertAsync(data);
login.Id = data.Id;
}
else
{
2017-01-03 08:17:15 +03:00
await _loginRepository.UpdateAsync(data);
}
2016-05-02 09:52:09 +03:00
}
else if(response.StatusCode == System.Net.HttpStatusCode.Forbidden
|| response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
{
MessagingCenter.Send(Application.Current, "Logout", (string)null);
}
2016-05-02 09:52:09 +03:00
return response;
2016-05-02 09:52:09 +03:00
}
2016-05-04 02:49:49 +03:00
public async Task<ApiResult> DeleteAsync(string id)
2016-05-04 02:49:49 +03:00
{
2017-01-03 08:17:15 +03:00
var response = await _loginApiRepository.DeleteAsync(id);
if(response.Succeeded)
2016-05-04 02:49:49 +03:00
{
2017-01-03 08:17:15 +03:00
await _loginRepository.DeleteAsync(id);
2016-05-04 02:49:49 +03:00
}
else if(response.StatusCode == System.Net.HttpStatusCode.Forbidden
|| response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
{
MessagingCenter.Send(Application.Current, "Logout", (string)null);
}
2016-05-04 02:49:49 +03:00
return response;
2016-05-04 02:49:49 +03:00
}
2016-05-02 09:52:09 +03:00
}
}