-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor on repository and add mutations
- Loading branch information
Diego Pereira
committed
Apr 3, 2021
1 parent
a9cffbe
commit 62eb2da
Showing
19 changed files
with
153 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace GraphQL.API.Mutations | ||
{ | ||
using HotChocolate.Types; | ||
|
||
[ExtendObjectType(Name = "Mutation")] | ||
public class CategoryMutation | ||
{ | ||
// TODO: Create methods | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
namespace GraphQL.API.Mutations | ||
{ | ||
using GraphQL.Core.Entities; | ||
using GraphQL.Core.Repositories; | ||
using HotChocolate; | ||
using HotChocolate.Types; | ||
using System.Threading.Tasks; | ||
|
||
[ExtendObjectType(Name = "Mutation")] | ||
public class ProductMutation | ||
{ | ||
public Task<Product> CreateProductAsync(Product product, [Service] IProductRepository productRepository) => | ||
productRepository.InsertAsync(product); | ||
|
||
public Task<bool> RemoveProductAsync(string id, [Service] IProductRepository productRepository) => | ||
productRepository.RemoveAsync(id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace GraphQL.API.Queries | ||
{ | ||
using HotChocolate.Types; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
[ExtendObjectType(Name = "Query")] | ||
public class CategoryQuery | ||
{ | ||
// Create methods | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,7 @@ | ||
namespace GraphQL.Core.Entities | ||
{ | ||
using MongoDB.Bson; | ||
using MongoDB.Bson.Serialization.Attributes; | ||
|
||
public class Category | ||
public class Category : EntityBase | ||
{ | ||
[BsonId] | ||
[BsonRepresentation(BsonType.ObjectId)] | ||
public string Id { get; set; } | ||
public string Description { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace GraphQL.Core.Entities | ||
{ | ||
using MongoDB.Bson; | ||
using MongoDB.Bson.Serialization.Attributes; | ||
|
||
public class EntityBase | ||
{ | ||
[BsonId] | ||
[BsonRepresentation(BsonType.ObjectId)] | ||
public string Id { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace GraphQL.Core.Repositories | ||
{ | ||
using GraphQL.Core.Entities; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
|
||
public interface IBaseRepository<T> where T : EntityBase | ||
{ | ||
Task<IEnumerable<T>> GetAllAsync(); | ||
Task<T> GetByIdAsync(string id); | ||
Task<T> InsertAsync(T entity); | ||
Task<bool> RemoveAsync(string id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
namespace GraphQL.Core.Repositories | ||
{ | ||
using GraphQL.Core.Entities; | ||
using System.Threading.Tasks; | ||
|
||
public interface ICategoryRepository | ||
public interface ICategoryRepository : IBaseRepository<Category> | ||
{ | ||
Task<Category> GetById(string id); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,9 @@ | ||
namespace GraphQL.Core.Repositories | ||
{ | ||
using GraphQL.Core.Entities; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
|
||
public interface IProductRepository | ||
public interface IProductRepository : IBaseRepository<Product> | ||
{ | ||
Task<IEnumerable<Product>> GetAllAsync(); | ||
Task<Product> GetByIdAsync(string id); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,24 @@ | ||
namespace GraphQL.Infrastructure.Data | ||
{ | ||
using GraphQL.Core.Entities; | ||
using GraphQL.Infrastructure.Configurations; | ||
using MongoDB.Driver; | ||
|
||
public class CatalogContext : ICatalogContext | ||
{ | ||
private const string ProductCollectionName = "Products"; | ||
private const string CategoryCollectionName = "Categories"; | ||
private readonly IMongoDatabase database; | ||
|
||
public CatalogContext(MongoDbConfiguration mongoDbConfiguration) | ||
{ | ||
var client = new MongoClient(mongoDbConfiguration.ConnectionString); | ||
var database = client.GetDatabase(mongoDbConfiguration.Database); | ||
|
||
this.Categories = database.GetCollection<Category>(CategoryCollectionName); | ||
this.Products = database.GetCollection<Product>(ProductCollectionName); | ||
this.database = client.GetDatabase(mongoDbConfiguration.Database); | ||
|
||
CatalogContextSeed.SeedData(this.Categories, this.Products); | ||
CatalogContextSeed.SeedData(this.database); | ||
} | ||
|
||
public IMongoCollection<Category> Categories { get; } | ||
public IMongoCollection<Product> Products { get; } | ||
public IMongoCollection<T> GetCollection<T>(string name) | ||
{ | ||
return this.database.GetCollection<T>(name); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,9 @@ | ||
namespace GraphQL.Infrastructure.Data | ||
{ | ||
using GraphQL.Core.Entities; | ||
using MongoDB.Driver; | ||
|
||
public interface ICatalogContext | ||
{ | ||
IMongoCollection<Category> Categories { get; } | ||
IMongoCollection<Product> Products { get; } | ||
IMongoCollection<T> GetCollection<T>(string name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
namespace GraphQL.Infrastructure.Repositories | ||
{ | ||
using GraphQL.Core.Entities; | ||
using GraphQL.Core.Repositories; | ||
using GraphQL.Infrastructure.Data; | ||
using MongoDB.Driver; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
|
||
public class BaseRepository<T> : IBaseRepository<T> where T : EntityBase | ||
{ | ||
private readonly IMongoCollection<T> collection; | ||
|
||
public BaseRepository(ICatalogContext catalogContext) | ||
{ | ||
if (catalogContext == null) | ||
{ | ||
throw new ArgumentNullException(nameof(catalogContext)); | ||
} | ||
|
||
this.collection = catalogContext.GetCollection<T>(typeof(T).Name); | ||
} | ||
|
||
public async Task<IEnumerable<T>> GetAllAsync() | ||
{ | ||
return await this.collection.Find(_ => true).ToListAsync(); | ||
} | ||
|
||
public async Task<T> GetByIdAsync(string id) | ||
{ | ||
var filter = Builders<T>.Filter.Eq(_ => _.Id, id); | ||
|
||
return await this.collection.Find(filter).FirstOrDefaultAsync(); | ||
} | ||
|
||
public async Task<T> InsertAsync(T entity) | ||
{ | ||
await this.collection.InsertOneAsync(entity); | ||
|
||
return entity; | ||
} | ||
|
||
public async Task<bool> RemoveAsync(string id) | ||
{ | ||
var result = await this.collection.DeleteOneAsync(Builders<T>.Filter.Eq(_ => _.Id, id)); | ||
|
||
return result.DeletedCount > 0; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters