Skip to content

Commit

Permalink
Sample blog application (#9)
Browse files Browse the repository at this point in the history
* Added sample launch settings.

* Sample application with rest and squadron.
  • Loading branch information
nscheibe authored Dec 6, 2019
1 parent cd94cb7 commit 3272250
Show file tree
Hide file tree
Showing 33 changed files with 486 additions and 84 deletions.
25 changes: 25 additions & 0 deletions samples/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
19 changes: 10 additions & 9 deletions samples/Context/DataAccess/BlogRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,31 @@
using Models;
using MongoDB.Driver;

namespace DataAccess
namespace SimpleBlog.DataAccess
{
public class BlogRepository
public class BlogRepository : IBlogRepository
{
private InsertOneOptions _insertOneOptions;
private IMongoCollection<Blog> _mongoCollection;

public BlogRepository(SimpleBlogDbContext simpleBlogDbContext)
public BlogRepository(ISimpleBlogDbContext simpleBlogDbContext)
{
if (simpleBlogDbContext == null)
throw new ArgumentNullException(nameof(simpleBlogDbContext));

_mongoCollection = simpleBlogDbContext.CreateCollection<Blog>();
}

public async Task AddBlogAsync(
Blog blog, CancellationToken cancellationToken)
{
var insertOneOptions = new InsertOneOptions()
_insertOneOptions = new InsertOneOptions()
{
BypassDocumentValidation = false
};
}

public async Task AddBlogAsync(
Blog blog, CancellationToken cancellationToken)
{
await _mongoCollection
.InsertOneAsync(blog, insertOneOptions, cancellationToken);
.InsertOneAsync(blog, _insertOneOptions, cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
using Models;
using System;
using Models;
using MongoDB.Driver;
using MongoDB.Extensions.Context;

namespace DataAccess
namespace SimpleBlog.DataAccess
{
internal class BlogCollectionConfiguration : IMongoCollectionConfiguration<Blog>
{
public void Configure(IMongoCollectionBuilder<Blog> mongoCollectionBuilder)
public void OnConfiguring(IMongoCollectionBuilder<Blog> mongoCollectionBuilder)
{
mongoCollectionBuilder
.WithCollectionName("blogs")
.AddBsonClassMap<Blog>(cm =>
{
cm.AutoMap();
cm.MapIdMember<string>(c => c.Id);
cm.MapIdMember<Guid>(c => c.Id);
})
.WithMongoCollectionSettings(settings => settings.ReadConcern = ReadConcern.Majority)
.WithMongoCollectionSettings(settings => settings.ReadPreference = ReadPreference.Nearest)
.WithMongoCollectionConfiguration(collection =>
.WithCollectionSettings(settings => settings.ReadConcern = ReadConcern.Majority)
.WithCollectionSettings(settings => settings.ReadPreference = ReadPreference.Nearest)
.WithCollectionConfiguration(collection =>
{
var timestampIndex = new CreateIndexModel<Blog>(
Builders<Blog>.IndexKeys.Ascending(blog => blog.TimeStamp),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@
using MongoDB.Extensions.Context;
using Tag = Models.Tag;

namespace DataAccess
namespace SimpleBlog.DataAccess
{
internal class TagCollectionConfiguration : IMongoCollectionConfiguration<Tag>
{
public void Configure(IMongoCollectionBuilder<Tag> mongoCollectionBuilder)
public void OnConfiguring(IMongoCollectionBuilder<Tag> mongoCollectionBuilder)
{
mongoCollectionBuilder
.AddBsonClassMap<Tag>(cm => cm.AutoMap())
.WithMongoCollectionSettings(setting =>
.WithCollectionSettings(setting =>
{
setting.ReadPreference = ReadPreference.Nearest;
setting.ReadConcern = ReadConcern.Available;
setting.WriteConcern = WriteConcern.Acknowledged;
})
.WithMongoCollectionConfiguration(collection =>
.WithCollectionConfiguration(collection =>
{
var timestampIndex = new CreateIndexModel<Tag>(
Builders<Tag>.IndexKeys.Ascending(tag => tag.Name),
new CreateIndexOptions { Unique = false });
new CreateIndexOptions { Unique = true });
collection.Indexes.CreateOne(timestampIndex);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
using MongoDB.Driver;
using MongoDB.Extensions.Context;

namespace DataAccess
namespace SimpleBlog.DataAccess
{
internal class UserCollectionConfiguration : IMongoCollectionConfiguration<User>
{
public void Configure(IMongoCollectionBuilder<User> mongoCollectionBuilder)
public void OnConfiguring(IMongoCollectionBuilder<User> mongoCollectionBuilder)
{
mongoCollectionBuilder
.WithCollectionName("users")
.AddBsonClassMap<User>(ConfigureUserClassMap())
.WithMongoCollectionSettings(ConfigureCollectionSettings())
.WithMongoCollectionConfiguration(ConfigureIndexes());
.WithCollectionSettings(ConfigureCollectionSettings())
.WithCollectionConfiguration(ConfigureIndexes());
}

private static Action<MongoCollectionSettings> ConfigureCollectionSettings()
Expand Down Expand Up @@ -57,7 +57,6 @@ private static Action<BsonClassMap<User>> ConfigureUserClassMap()
return cm =>
{
cm.AutoMap();
cm.MapIdMember(c => c.Id);
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion samples/Context/DataAccess/DataAccess.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.0" />
<PackageReference Include="MongoDB.Extensions.Context" Version="0.1.0-preview1" />
<PackageReference Include="MongoDB.Extensions.Context" Version="0.1.0-preview3" />
</ItemGroup>

<ItemGroup>
Expand Down
11 changes: 11 additions & 0 deletions samples/Context/DataAccess/IBlogRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Threading;
using System.Threading.Tasks;
using Models;

namespace SimpleBlog.DataAccess
{
public interface IBlogRepository
{
Task AddBlogAsync(Blog blog, CancellationToken cancellationToken = default);
}
}
12 changes: 12 additions & 0 deletions samples/Context/DataAccess/ITagRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Models;

namespace SimpleBlog.DataAccess
{
public interface ITagRepository
{
Task TryAddTagsAsync(IEnumerable<Tag> newTags, CancellationToken cancellationToken = default);
}
}
13 changes: 13 additions & 0 deletions samples/Context/DataAccess/IUserRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Models;

namespace SimpleBlog.DataAccess
{
public interface IUserRepository
{
Task AddUserAsync(User user, CancellationToken cancellationToken = default);
Task AttachBlogToUserAsync(string userId, Guid blogId, CancellationToken cancellationToken = default);
}
}
17 changes: 9 additions & 8 deletions samples/Context/DataAccess/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@
using Microsoft.Extensions.DependencyInjection;
using MongoDB.Extensions.Context;

namespace DataAccess
namespace SimpleBlog.DataAccess
{
public static class ServiceCollectionExtensions
{
private static IServiceCollection AddShopDatabase(
public static IServiceCollection AddBlogDatabase(
this IServiceCollection services, IConfiguration configuration)
{
MongoOptions shopDbOptions = configuration
.GetSection("Shop:Database")
.Get<MongoOptions>();
MongoOptions blogDbOptions = configuration
.GetMongoOptions(WellKnown.Path.SimpleBlogDB);

services.AddSingleton<MongoOptions>(sp => shopDbOptions);
services.AddSingleton<SimpleBlogDbContext>();
services.AddSingleton<BlogRepository>();
services.AddSingleton(blogDbOptions);
services.AddSingleton<ISimpleBlogDbContext, SimpleBlogDbContext>();
services.AddSingleton<IBlogRepository, BlogRepository>();
services.AddSingleton<IUserRepository, UserRepository>();
services.AddSingleton<ITagRepository, TagRepository>();

return services;
}
Expand Down
8 changes: 6 additions & 2 deletions samples/Context/DataAccess/SimpleBlogDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
using MongoDB.Driver;
using MongoDB.Extensions.Context;

namespace DataAccess
namespace SimpleBlog.DataAccess
{
public class SimpleBlogDbContext : MongoDbContext
public class SimpleBlogDbContext : MongoDbContext, ISimpleBlogDbContext
{
public SimpleBlogDbContext(MongoOptions mongoOptions) : base(mongoOptions)
{
Expand All @@ -23,4 +23,8 @@ protected override void OnConfiguring(IMongoDatabaseBuilder mongoDatabaseBuilder
.ConfigureCollection(new TagCollectionConfiguration());
}
}

public interface ISimpleBlogDbContext : IMongoDbContext
{
}
}
31 changes: 19 additions & 12 deletions samples/Context/DataAccess/TagRepository.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,41 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Driver;
using Tag = Models.Tag;

namespace DataAccess
namespace SimpleBlog.DataAccess
{
public class TagRepository
public class TagRepository : ITagRepository
{
private InsertOneOptions _insertOneOptions;
private InsertManyOptions _insertManyOptions;
private IMongoCollection<Tag> _mongoCollection;
public TagRepository(SimpleBlogDbContext simpleBlogDbContext)

public TagRepository(ISimpleBlogDbContext simpleBlogDbContext)
{
if (simpleBlogDbContext == null)
throw new ArgumentNullException(nameof(simpleBlogDbContext));

_mongoCollection = simpleBlogDbContext.CreateCollection<Tag>();

_insertOneOptions = new InsertOneOptions()
_insertManyOptions = new InsertManyOptions()
{
BypassDocumentValidation = false
BypassDocumentValidation = false,
IsOrdered = false
};
}
public async Task AddTagAsync(
Tag tag, CancellationToken cancellationToken)

public async Task TryAddTagsAsync(
IEnumerable<Tag> tags, CancellationToken cancellationToken)
{
await _mongoCollection
.InsertOneAsync(tag, _insertOneOptions, cancellationToken);
//FilterDefinition<Tag> filter = Builders<Tag>.Filter
// .Eq(t => t.Name, tenantName);

//Builders<Tag>.Update.SetOnInsert()

//await _mongoCollection
// .UpdateManyAsync(tags, _insertManyOptions, cancellationToken);
}
}
}
16 changes: 11 additions & 5 deletions samples/Context/DataAccess/UserRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
using Models;
using MongoDB.Driver;

namespace DataAccess
namespace SimpleBlog.DataAccess
{
public class UserRepository
public class UserRepository : IUserRepository
{
private InsertOneOptions _insertOneOptions;
private IMongoCollection<User> _mongoCollection;
public UserRepository(SimpleBlogDbContext simpleBlogDbContext)

public UserRepository(ISimpleBlogDbContext simpleBlogDbContext)
{
if (simpleBlogDbContext == null)
throw new ArgumentNullException(nameof(simpleBlogDbContext));
Expand All @@ -23,12 +23,18 @@ public UserRepository(SimpleBlogDbContext simpleBlogDbContext)
BypassDocumentValidation = false
};
}

public async Task AddUserAsync(
User user, CancellationToken cancellationToken)
{
await _mongoCollection
.InsertOneAsync(user, _insertOneOptions, cancellationToken);
}

public async Task AttachBlogToUserAsync(
string userId, Guid blogId, CancellationToken cancellationToken = default)
{
//await _mongoCollection.UpdateOneAsync()
}
}
}
14 changes: 14 additions & 0 deletions samples/Context/DataAccess/WellKnown.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace SimpleBlog.DataAccess
{
public static class WellKnown
{
public static class Path
{
public const string SimpleBlogDB = "SimpleBlog:Database";
}
}
}
Loading

0 comments on commit 3272250

Please sign in to comment.