Implementation to have a favorite DZ by user.
This commit is contained in:
@@ -4,15 +4,17 @@ using System.Collections.Generic;
|
||||
using skydiveLogs_api.DomainBusiness.Interfaces;
|
||||
using skydiveLogs_api.DomainService.Repositories;
|
||||
using skydiveLogs_api.Domain;
|
||||
|
||||
using System.Linq;
|
||||
|
||||
namespace skydiveLogs_api.DomainBusiness
|
||||
{
|
||||
public class DropZoneService : IDropZoneService
|
||||
{
|
||||
public DropZoneService(IDropZoneRepository dropZoneRepository)
|
||||
public DropZoneService(IDropZoneRepository dropZoneRepository,
|
||||
IFavoriteDropZoneRepository favoriteDropZoneRepository)
|
||||
{
|
||||
_dropZoneRepository = dropZoneRepository;
|
||||
_favoriteDropZoneRepository = favoriteDropZoneRepository;
|
||||
}
|
||||
|
||||
public void AddNewDz(DropZone newdropZone)
|
||||
@@ -25,9 +27,31 @@ namespace skydiveLogs_api.DomainBusiness
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public IEnumerable<DropZone> GetAllDzs()
|
||||
public IEnumerable<DropZone> GetAllDzs(User connectedUser)
|
||||
{
|
||||
return _dropZoneRepository.GetAll();
|
||||
var results = Enumerable.Empty<DropZone>();
|
||||
|
||||
var dropzones = _dropZoneRepository.GetAll();
|
||||
var favorites = _favoriteDropZoneRepository.GetAll(connectedUser);
|
||||
|
||||
|
||||
results = from dropZone in dropzones
|
||||
join favorite in favorites on dropZone equals favorite.DropZone into tmp
|
||||
from favoriteDz in tmp.DefaultIfEmpty()
|
||||
select new DropZone
|
||||
{
|
||||
Id = dropZone.Id,
|
||||
Address = dropZone.Address,
|
||||
Email = dropZone.Email,
|
||||
Latitude = dropZone.Latitude,
|
||||
Longitude = dropZone.Longitude,
|
||||
Name = dropZone.Name,
|
||||
Type = dropZone.Type,
|
||||
Website = dropZone.Website,
|
||||
IsFavorite = !(favoriteDz == null)
|
||||
};
|
||||
|
||||
return results.ToList();
|
||||
}
|
||||
|
||||
public DropZone GetDzById(int id)
|
||||
@@ -42,6 +66,30 @@ namespace skydiveLogs_api.DomainBusiness
|
||||
return _dropZoneRepository.Update(dropZone);
|
||||
}
|
||||
|
||||
public bool AddToFavorite(int dzId, User connectedUser)
|
||||
{
|
||||
var dzToAddToFavorite = GetDzById(dzId);
|
||||
|
||||
var favoriteDz = new FavoriteDropZone
|
||||
{
|
||||
DropZone = dzToAddToFavorite,
|
||||
User = connectedUser
|
||||
};
|
||||
|
||||
var result = _favoriteDropZoneRepository.Add(favoriteDz);
|
||||
|
||||
return result > 0;
|
||||
}
|
||||
|
||||
public bool RemoveToFavorite(int dzId, User connectedUser)
|
||||
{
|
||||
var result = _favoriteDropZoneRepository.Delete(dzId, connectedUser.Id);
|
||||
|
||||
return result > 0;
|
||||
}
|
||||
|
||||
private readonly IDropZoneRepository _dropZoneRepository;
|
||||
|
||||
private readonly IFavoriteDropZoneRepository _favoriteDropZoneRepository;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ namespace skydiveLogs_api.DomainBusiness.Interfaces
|
||||
{
|
||||
public interface IDropZoneService
|
||||
{
|
||||
IEnumerable<DropZone> GetAllDzs();
|
||||
IEnumerable<DropZone> GetAllDzs(User connectedUser);
|
||||
|
||||
DropZone GetDzById(int id);
|
||||
|
||||
@@ -16,5 +16,9 @@ namespace skydiveLogs_api.DomainBusiness.Interfaces
|
||||
bool UpdateDz(int id, DropZone dropZone);
|
||||
|
||||
void AddNewDz(DropZone dropZone);
|
||||
|
||||
bool AddToFavorite(int dzId, User connectedUser);
|
||||
|
||||
bool RemoveToFavorite(int dzId, User connectedUser);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user