Skip to content

Commit

Permalink
Fix bugs (#235)
Browse files Browse the repository at this point in the history
* create new project for Services testing

* Add query parameter classes

* Add PagedList class

* Add pagination methods to services and repos

* Added unit tests for FoodEFRepository methods

* Implement food api get method with pagination

* Add more incomplete changes

* Add test for RestaurantEFRepository

* made folders for tests

* Restructure, remove old repos, implement services

* Implement updated repositories

* Remove all code from console app

* Change get metods in repos to take order delegate

* Revert changes to get methods in repos

* Implement pagination in restaurant controller

* Fix bugs, add new function to food repo

* Add inefficient sorting

* Fix bug

* fixed compiling issues

* Fix GetRestaurantsCloserThan function

* Fixed bug in GetAllRestaurantsCloserThan

* Fix bug

* Add methods to typeOfFoodRepository

* Added unit test for TypeOfFoodEFRepository

* Tests refactor

* Move id generation to persistence layer

* Added some tests for FoodServices

* Add typeOfFood api methods

* Added more tests for FoodServce

* Remove interface to abstract DbContext

* Fix get and insert methods in food repo

* Fix url and description bugs

* Fix restaurant food list not showing types of food

* Added tests for RestaurantServices

* Partially fix update methods

* Fix bug when updating not existing entity

* Update not passing tests

* Fix pagedListExtensions bug

* Fix sorting by distance, move dto mapping to mappers

* Add foods property to restaurant model and fix sorting

* Add foods property to restaurant model and fix sorting

Co-authored-by: DominykasSip <[email protected]>
  • Loading branch information
DonatasPuc and DominykasSip authored Dec 6, 2021
1 parent 0577ccf commit 1fff3a5
Show file tree
Hide file tree
Showing 20 changed files with 739 additions and 85 deletions.
24 changes: 1 addition & 23 deletions backend/Contracts/DTOs/FoodResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Contracts.DTOs
{
public class FoodResponse : BaseDto
{
private FoodResponse(string id, string name) : base(id, name)
public FoodResponse(string id, string name) : base(id, name)
{
}

Expand All @@ -23,27 +23,5 @@ private FoodResponse(string id, string name) : base(id, name)
public decimal AmountPerInterval { get; set; }
public double PercentPerInterval { get; set; }
public string ImageURL { get; set; }

public static FoodResponse FromEntity(Food food)
{
_ = food ?? throw new ArgumentNullException(nameof(food));

return new FoodResponse(food.Id, food.Name)
{
StartingPrice = food.StartingPrice,
MinimumPrice = food.MinimumPrice,
CurrentPrice = food.CalculateCurrentPrice(),
CreatedAt = food.CreatedAt,
IdRestaurant = food.IdRestaurant,
TypesOfFood = food.TypesOfFood,
StartDecreasingAt = food.StartDecreasingAt,
IntervalTimeInMinutes = food.IntervalTimeInMinutes,
AmountPerInterval = food.AmountPerInterval,
PercentPerInterval = food.PercentPerInterval,
DecreaseType = food.DecreaseType,
Description = food.Description,
ImageURL = food.ImageURL
};
}
}
}
19 changes: 4 additions & 15 deletions backend/Contracts/DTOs/RestaurantDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ public RestaurantDto
string name,
string address,
Coords coords,
double distanceToUser,
int foodCount,
string description = "",
string imageURL = ""
)
: base(id, name)
{
FoodCount = foodCount;
DistanceToUser = distanceToUser;
Description = description;
Address = address;
Coords = coords ?? throw new ArgumentNullException(nameof(coords));
Expand All @@ -39,20 +43,5 @@ public RestaurantDto
ImageURL = imageURL;
}
}

public static RestaurantDto FromEntity(Restaurant restaurant)
{
_ = restaurant ?? throw new ArgumentNullException(nameof(restaurant));

return new RestaurantDto
(
restaurant.Id,
restaurant.Name,
restaurant.Address,
restaurant.Coords,
restaurant.Description,
restaurant.ImageURL
);
}
}
}
2 changes: 1 addition & 1 deletion backend/Domain/Models/Extensions/PagedListExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static class PagedListExtensions
public static PagedList<TOutput> ConvertAllItems<T,TOutput>(this PagedList<T> source, Converter<T,TOutput> converter)
{
var convertedList = source.Select(x => converter(x)).ToList();
return new PagedList<TOutput>(convertedList, source.Count, source.CurrentPage, source.PageSize);
return new PagedList<TOutput>(convertedList, source.TotalCount, source.CurrentPage, source.PageSize);
}
}
}
11 changes: 10 additions & 1 deletion backend/Domain/Models/Restaurant.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using Domain.Helpers;
using System;
using System.Collections.Generic;

namespace Domain.Models
{
public class Restaurant : BaseModel
{
public IEnumerable<Food> Foods { get; set; }
public Coords Coords { get; set; }
public string Address { get; set; }
public Credentials Credentials { get; set; }
Expand All @@ -15,9 +17,10 @@ public class Restaurant : BaseModel

public Restaurant() : base() { }

public Restaurant(string id, string name, string address, Coords coords, Credentials credentials, string description = "", string imageURL = "")
public Restaurant(string id, string name, string address, Coords coords, Credentials credentials, IEnumerable<Food> foods, string description = "", string imageURL = "")
: base(id, name)
{
Foods = foods;
Description = description;
Address = address;
Coords = coords ?? throw new ArgumentNullException(nameof(coords));
Expand Down Expand Up @@ -45,5 +48,11 @@ public bool IsCloser(Coords coords, Distances distance)

return CoordsHelper.IsCloser(Coords, coords, distance);
}

public double DistanceTo(Coords coords)
{
coords ??= new Coords { Longitude = 0, Latitude = 0 };
return CoordsHelper.HaversineDistanceKM(Coords, coords);
}
}
}
25 changes: 24 additions & 1 deletion backend/Persistence/Mappers/FoodMapper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Domain.Entities;
using Contracts.DTOs;
using Domain.Entities;
using Domain.Models;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -51,5 +52,27 @@ public static FoodEntity ToEntity(this Food from)
Description = from.Description,
};
}

public static FoodResponse ToFoodResponse(this Food from)
{
_ = from ?? throw new ArgumentNullException(nameof(from));

return new FoodResponse(from.Id, from.Name)
{
StartingPrice = from.StartingPrice,
MinimumPrice = from.MinimumPrice,
CurrentPrice = from.CalculateCurrentPrice(),
CreatedAt = from.CreatedAt,
IdRestaurant = from.IdRestaurant,
TypesOfFood = from.TypesOfFood,
StartDecreasingAt = from.StartDecreasingAt,
IntervalTimeInMinutes = from.IntervalTimeInMinutes,
AmountPerInterval = from.AmountPerInterval,
PercentPerInterval = from.PercentPerInterval,
DecreaseType = from.DecreaseType,
Description = from.Description,
ImageURL = from.ImageURL
};
}
}
}
26 changes: 24 additions & 2 deletions backend/Persistence/Mappers/RestaurantMapper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Domain.Entities;
using Contracts.DTOs;
using Domain.Entities;
using Domain.Models;
using System;
using System.Collections.Generic;
Expand All @@ -12,6 +13,8 @@ public static class RestaurantMapper
{
public static Restaurant ToDomain(this RestaurantEntity from)
{
_ = from ?? throw new ArgumentNullException(nameof(from));

string restaurantId = from.Id.ToString();
Coords coords = new Coords { Longitude = from.Longitude, Latitude = from.Latitude };
Credentials credentials = new Credentials
Expand All @@ -26,13 +29,15 @@ public static Restaurant ToDomain(this RestaurantEntity from)
from.Address,
coords,
credentials,
from.Foods.Select(x => x.ToDomain()),
from.Description,
from.ImageURL);
}

public static RestaurantEntity ToEntity(this Restaurant from)
{
// FIX: foods not set
_ = from ?? throw new ArgumentNullException(nameof(from));

return new RestaurantEntity
{
Id = Guid.Parse(from.Id),
Expand All @@ -46,5 +51,22 @@ public static RestaurantEntity ToEntity(this Restaurant from)
ImageURL = from.ImageURL,
};
}

public static RestaurantDto ToDTO(this Restaurant from, Coords coords = null)
{
_ = from ?? throw new ArgumentNullException(nameof(from));

return new RestaurantDto
(
from.Id,
from.Name,
from.Address,
from.Coords,
from.DistanceTo(coords),
from.Foods.Count(),
from.Description,
from.ImageURL
);
}
}
}
6 changes: 3 additions & 3 deletions backend/Persistence/Repositories/RestaurantEFRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void Delete(string id)

public IQueryable<Restaurant> GetAll()
{
return _context.Restaurants.Select(x => x.ToDomain());
return _context.Restaurants.Include(x => x.Foods).Select(x => x.ToDomain());
}

public IQueryable<Restaurant> GetAllRestaurantsCloserThan(Coords coords, Distances distance)
Expand All @@ -52,7 +52,7 @@ public Restaurant GetById(string id)

public Restaurant GetByMail(Mail mail)
{
return _context.Restaurants.FirstOrDefault(x => x.Mail == mail.Value)?.ToDomain();
return _context.Restaurants.Include(x => x.Foods).FirstOrDefault(x => x.Mail == mail.Value)?.ToDomain();
}

public IQueryable<Restaurant> GetRestaurantsNear(Coords coords)
Expand All @@ -78,7 +78,7 @@ public void Update(Restaurant restaurant)

private RestaurantEntity GetByIdString(string id)
{
return _context.Restaurants.Find(Guid.Parse(id));
return _context.Restaurants.Include(x => x.Foods).FirstOrDefault(x => x.Id == Guid.Parse(id));
}
}
}
1 change: 0 additions & 1 deletion backend/Persistence/Repositories/TypeOfFoodEFRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public TypeOfFoodEFRepository(DatabaseContext context)
public string Insert(TypeOfFood typeOfFood)
{
typeOfFood.Id = IdGenerator.GenerateUniqueId();

_context.TypesOfFood.Add(typeOfFood.ToEntity());
_context.SaveChanges();
return typeOfFood.Id;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
using Domain.Entities;
using Domain.Models;
using Services.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace Services.Tests.RepositoriesUnitTests
{
public class FoodEFRepositoryUnitTests : UnitTests
{
private FoodEntity FoodEntity { get; set; }
private Food Food { get; set; }

public FoodEFRepositoryUnitTests()
{
FoodEntity = GetSampleFoodEntity();
Food = GetSampleFood();
}


[Fact]
public void Insert_InsertFoodToDataBase()
{
var sut = new FoodEFRepository(_context);

sut.Insert(Food);

List<FoodEntity> foods = _context.Foods.ToList();
Assert.Single(foods);
Assert.Equal(Food.Id, foods.First().Id.ToString());
}

[Fact]
public void Delete_DeletesExistingFood()
{
_context.Foods.Add(FoodEntity);
_context.SaveChanges();
var sut = new FoodEFRepository(_context);

sut.Delete(FoodEntity.Id.ToString());

Assert.Empty(_context.Foods.ToList());
}

[Fact]
public void GetAll_OneFoodInDataBase_GetsAllExistingFoods()
{
_context.Foods.Add(FoodEntity);
_context.SaveChanges();
var sut = new FoodEFRepository(_context);

Assert.Single(sut.GetAll());
}

[Fact]
public void GetAll_MoreThanOneFoodInDataBase_GetsAllExistingFoods()
{
_context.Foods.Add(FoodEntity);
var otherFoodId = Guid.NewGuid();
var otherTypeId = Guid.NewGuid();
var otherFood = GetSampleFoodEntity();
otherFood.Id = otherFoodId;
otherFood.TypesOfFood.First().Id = otherTypeId;
_context.Foods.Add(otherFood);
_context.SaveChanges();
var sut = new FoodEFRepository(_context);

IQueryable<Food> foods = sut.GetAll();

Assert.NotEmpty(foods);
Assert.Equal(FoodEntity.Id.ToString(), foods.ToList()[0].Id);
Assert.Equal(otherFoodId.ToString(), foods.ToList()[1].Id);
}

[Fact]
public void GetById_FoodExistsInDataBase_ReturnsFood()
{
_context.Foods.Add(FoodEntity);
_context.SaveChanges();
var sut = new FoodEFRepository(_context);

var food = sut.GetById(FoodEntity.Id.ToString());

Assert.Equal(FoodEntity.Id.ToString(), food.Id);
}

[Fact]
public void GetById_FoodDoesntExistsInDataBase_ReturnsNull()
{
var sut = new FoodEFRepository(_context);

var food = sut.GetById(Guid.NewGuid().ToString());

Assert.Null(food);
}

[Fact]
public void Update_UpdateExistingFood()
{
_context.Foods.Add(FoodEntity);
_context.SaveChanges();
var sut = new FoodEFRepository(_context);
var newFoodName = "NewFoodTestName";
var food = new Food
{
Id = FoodEntity.Id.ToString(),
IdRestaurant = FoodEntity.RestaurantId.ToString(),
Name = newFoodName,
TypesOfFood = GetSampleTypesOfFoodList(),
PercentPerInterval = 1
};
food.TypesOfFood.First().Id = Guid.NewGuid().ToString();

sut.Update(food);

var UpdatedFood = _context.Foods.Find(FoodEntity.Id);
Assert.Equal(FoodEntity.Id, UpdatedFood.Id);
Assert.NotEqual(FoodEntity.Name, UpdatedFood.Name);
}

[Fact]
public void Update_UpdateNotExistingFood()
{
_context.Foods.Add(FoodEntity);
_context.SaveChanges();
var sut = new FoodEFRepository(_context);
var newFoodName = "NewFoodTestName";
var food = new Food
{
Id = Guid.NewGuid().ToString(),
IdRestaurant = FoodEntity.Id.ToString(),
Name = newFoodName,
TypesOfFood = GetSampleTypesOfFoodList(),
PercentPerInterval = 1
};

sut.Update(food);

var foodFormDB = _context.Foods.Find(FoodEntity.Id);
Assert.Equal(FoodEntity.Name, foodFormDB.Name);
}
}
}
Loading

0 comments on commit 1fff3a5

Please sign in to comment.