Skip to content

Commit ed44130

Browse files
author
Ray Fan
authored
Merge pull request #52 from FanrayMedia/dev
1.0.0-beta2 release from dev to master
2 parents 0cbf1b6 + 27d5364 commit ed44130

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+390
-373
lines changed

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Either way you will see the blog setup page on app initial launch.
2020
## Database
2121

2222
Database is created for you on app initial launch. Out of box it uses SQLite, and SQL Server is also supported.
23-
To switch to SQL Server, go to `appsettings.json` you will see the following.
23+
To switch to SQL Server, go to `appsettings.json` and change the Database value on line 3 from `sqlite` to `sqlserver` and update your connection string accordingly.
2424

2525
```
2626
"AppSettings": {
@@ -32,11 +32,8 @@ To switch to SQL Server, go to `appsettings.json` you will see the following.
3232
"ConnectionStrings": {
3333
"DefaultConnection": "Server=.\\SQLEXPRESS;Database=Fanray;Trusted_Connection=True;MultipleActiveResultSets=true"
3434
},
35-
3635
```
3736

38-
Change `sqlite` to `sqlserver` and update your connection string accordingly.
39-
4037
## Open Live Writer
4138

4239
Right now the only way to post is through [Open Live Writer](http://openlivewriter.org/). To get started,

src/Fan.Blogs/Data/BlogDbContext.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,20 @@
55

66
namespace Fan.Blogs.Data
77
{
8+
/// <summary>
9+
/// The DbContext for blog application.
10+
/// </summary>
11+
/// <remarks>
12+
/// It's deriving from IdentityDbContext instead of DbContext because when testing it's still
13+
/// required to access User table to seed data.
14+
/// </remarks>
815
public class BlogDbContext : IdentityDbContext<User, Role, int>
916
{
1017
public virtual DbSet<Post> Posts { get; set; }
1118
public virtual DbSet<Category> Categories { get; set; }
1219
public virtual DbSet<Tag> Tags { get; set; }
1320
public virtual DbSet<PostTag> PostTags { get; set; }
21+
public virtual DbSet<Media> Medias { get; set; }
1422

1523
public BlogDbContext(DbContextOptions<BlogDbContext> options)
1624
: base(options)
@@ -58,6 +66,12 @@ public static void CreateBlogModel(ModelBuilder builder)
5866
entity.ToTable("Blog_PostTag");
5967
entity.HasKey(e => new { e.PostId, e.TagId });
6068
});
69+
70+
builder.Entity<Media>(entity =>
71+
{
72+
entity.ToTable("Blog_Media");
73+
entity.HasIndex(e => new { e.Type, e.UploadedOn });
74+
});
6175
}
6276
}
6377
}
Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Fan.Blogs.Models;
2+
using Fan.Data;
23
using Fan.Models;
34
using System.Collections.Generic;
45
using System.Threading.Tasks;
@@ -8,13 +9,8 @@ namespace Fan.Blogs.Data
89
/// <summary>
910
/// Contract for a category repository.
1011
/// </summary>
11-
public interface ICategoryRepository
12+
public interface ICategoryRepository : IRepository<Category>
1213
{
13-
/// <summary>
14-
/// Creates a new <see cref="Category"/>.
15-
/// </summary>
16-
Task<Category> CreateAsync(Category category);
17-
1814
/// <summary>
1915
/// Deletes a <see cref="Category"/> by id and re-categorize its posts to the given
2016
/// default category id.
@@ -30,11 +26,5 @@ public interface ICategoryRepository
3026
/// Returns all categories or empty list if no categories found.
3127
/// </summary>
3228
Task<List<Category>> GetListAsync();
33-
34-
/// <summary>
35-
/// Updates a <see cref="Category"/>.
36-
/// </summary>
37-
/// <param name="category">Not all implementations use this parameter, such as the Sql ones.</param>
38-
Task<Category> UpdateAsync(Category category);
3929
}
4030
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using Fan.Blogs.Models;
2+
using Fan.Data;
3+
using System;
4+
using System.Threading.Tasks;
5+
6+
namespace Fan.Blogs.Data
7+
{
8+
public interface IMediaRepository : IRepository<Media>
9+
{
10+
Task<Media> GetAsync(int mediaId);
11+
Task<Media> GetAsync(string fileName, DateTimeOffset uploadedOn);
12+
}
13+
}

src/Fan.Blogs/Data/IPostRepository.cs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using Fan.Blogs.Enums;
22
using Fan.Blogs.Models;
3-
using Fan.Enums;
4-
using Fan.Models;
3+
using Fan.Data;
54
using System.Collections.Generic;
65
using System.Threading.Tasks;
76

@@ -10,26 +9,14 @@ namespace Fan.Blogs.Data
109
/// <summary>
1110
/// Contract for a post repository.
1211
/// </summary>
13-
public interface IPostRepository
12+
public interface IPostRepository : IRepository<Post>
1413
{
15-
/// <summary>
16-
/// Creates a new <see cref="Post"/>
17-
/// </summary>
18-
Task<Post> CreateAsync(Post post);
19-
2014
/// <summary>
2115
/// Deletes a <see cref="Post"/> by Id, if the post is a root page,
2216
/// it will also delete all child pages.
2317
/// </summary>
2418
Task DeleteAsync(int id);
2519

26-
/// <summary>
27-
/// Updates a <see cref="Post"/>.
28-
/// </summary>
29-
/// <param name="post">Not all implementations use this parameter, such as the Sql ones.</param>
30-
/// <returns></returns>
31-
Task<Post> UpdateAsync(Post post);
32-
3320
/// <summary>
3421
/// Returns a <see cref="Post"/> by id. If it is a BlogPost it'll return together with its
3522
/// <see cref="Category"/> and <see cref="Tag"/>. Returns null if it's not found.

src/Fan.Blogs/Data/ITagRepository.cs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using Fan.Blogs.Models;
2-
using Fan.Models;
2+
using Fan.Data;
33
using System.Collections.Generic;
44
using System.Threading.Tasks;
55

@@ -8,15 +8,8 @@ namespace Fan.Blogs.Data
88
/// <summary>
99
/// Contract for a tag repository.
1010
/// </summary>
11-
public interface ITagRepository
11+
public interface ITagRepository : IRepository<Tag>
1212
{
13-
/// <summary>
14-
/// Creates a <see cref="Tag"/>.
15-
/// </summary>
16-
/// <param name="tag"></param>
17-
/// <returns></returns>
18-
Task<Tag> CreateAsync(Tag tag);
19-
2013
/// <summary>
2114
/// Deletes a tag and its associated posts.
2215
/// </summary>
@@ -26,11 +19,5 @@ public interface ITagRepository
2619
/// Returns all tags or empty list if no tags found.
2720
/// </summary>
2821
Task<List<Tag>> GetListAsync();
29-
30-
/// <summary>
31-
/// Updates a <see cref="Tag"/>.
32-
/// </summary>
33-
/// <param name="tag">Not all implementations use this parameter, such as the Sql ones.</param>
34-
Task<Tag> UpdateAsync(Tag tag);
3522
}
3623
}

src/Fan.Blogs/Data/SqlCategoryRepository.cs

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Fan.Blogs.Enums;
22
using Fan.Blogs.Models;
3+
using Fan.Data;
34
using Microsoft.EntityFrameworkCore;
45
using System.Collections.Generic;
56
using System.Linq;
@@ -10,24 +11,17 @@ namespace Fan.Blogs.Data
1011
/// <summary>
1112
/// Sql implementation of the <see cref="ICategoryRepository"/> contract.
1213
/// </summary>
13-
public class SqlCategoryRepository : ICategoryRepository
14+
/// <remarks>
15+
/// Category specific data access methods.
16+
/// </remarks>
17+
public class SqlCategoryRepository : EFRepository<Category>, ICategoryRepository
1418
{
1519
private readonly BlogDbContext _db;
16-
public SqlCategoryRepository(BlogDbContext db)
20+
public SqlCategoryRepository(BlogDbContext db) : base(db)
1721
{
1822
_db = db;
1923
}
2024

21-
/// <summary>
22-
/// Creates a new <see cref="Category"/>, the returned object is tracked.
23-
/// </summary>
24-
public async Task<Category> CreateAsync(Category category)
25-
{
26-
await _db.AddAsync(category);
27-
await _db.SaveChangesAsync();
28-
return category;
29-
}
30-
3125
/// <summary>
3226
/// Deletes a <see cref="Category"/> by id and re-categorize its posts to the given
3327
/// default category id.
@@ -73,15 +67,5 @@ public async Task<List<Category>> GetListAsync()
7367
Count = _db.Posts.Where(p => p.CategoryId == c.Id && p.Status == EPostStatus.Published).Count(),
7468
}).ToListAsync();
7569
}
76-
77-
/// <summary>
78-
/// Updates a <see cref="Category"/>.
79-
/// </summary>
80-
/// <param name="category">this parm is not used just being returned.</param>
81-
public async Task<Category> UpdateAsync(Category category)
82-
{
83-
await _db.SaveChangesAsync();
84-
return category;
85-
}
8670
}
8771
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using Fan.Blogs.Models;
2+
using Fan.Data;
3+
using Microsoft.EntityFrameworkCore;
4+
using System;
5+
using System.Threading.Tasks;
6+
7+
namespace Fan.Blogs.Data
8+
{
9+
public class SqlMediaRepository : EFRepository<Media>, IMediaRepository
10+
{
11+
private readonly BlogDbContext _db;
12+
public SqlMediaRepository(BlogDbContext db) : base(db)
13+
{
14+
_db = db;
15+
}
16+
17+
public async Task<Media> GetAsync(string fileName, DateTimeOffset uploadedOn)
18+
{
19+
return await _db.Medias.SingleOrDefaultAsync(m =>
20+
m.FileName.Equals(fileName, StringComparison.InvariantCultureIgnoreCase) &&
21+
m.UploadedOn.Year == uploadedOn.Year &&
22+
m.UploadedOn.Month == uploadedOn.Month);
23+
}
24+
25+
public async Task<Media> GetAsync(int mediaId)
26+
{
27+
return await _db.Medias.SingleAsync(m => m.Id == mediaId);
28+
}
29+
}
30+
}

src/Fan.Blogs/Data/SqlPostRepository.cs

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Fan.Blogs.Enums;
22
using Fan.Blogs.Models;
3+
using Fan.Data;
34
using Microsoft.EntityFrameworkCore;
45
using System;
56
using System.Collections.Generic;
@@ -11,26 +12,14 @@ namespace Fan.Blogs.Data
1112
/// <summary>
1213
/// Sql implementation of the <see cref="IPostRepository"/> contract.
1314
/// </summary>
14-
public class SqlPostRepository : IPostRepository
15+
public class SqlPostRepository : EFRepository<Post>, IPostRepository
1516
{
1617
private readonly BlogDbContext _db;
17-
public SqlPostRepository(BlogDbContext db)
18+
public SqlPostRepository(BlogDbContext db) : base(db)
1819
{
1920
_db = db;
2021
}
2122

22-
/// <summary>
23-
/// Creates a new <see cref="Post"/>.
24-
/// </summary>
25-
/// <param name="post"></param>
26-
/// <returns></returns>
27-
public async Task<Post> CreateAsync(Post post)
28-
{
29-
await _db.Posts.AddAsync(post);
30-
await _db.SaveChangesAsync();
31-
return post;
32-
}
33-
3423
/// <summary>
3524
/// Deletes a <see cref="Post"/> by Id, if the post is a root page,
3625
/// it will also delete all child pages.
@@ -166,16 +155,5 @@ from pt in p.PostTags
166155

167156
return (posts: posts, totalCount: postCount);
168157
}
169-
170-
/// <summary>
171-
/// Updates a post.
172-
/// </summary>
173-
/// <param name="post">Not used just being returned.</param>
174-
/// <returns></returns>
175-
public async Task<Post> UpdateAsync(Post post)
176-
{
177-
await _db.SaveChangesAsync();
178-
return post;
179-
}
180158
}
181159
}

src/Fan.Blogs/Data/SqlTagRepository.cs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Fan.Blogs.Enums;
22
using Fan.Blogs.Models;
3+
using Fan.Data;
34
using Microsoft.EntityFrameworkCore;
45
using System.Collections.Generic;
56
using System.Linq;
@@ -10,21 +11,14 @@ namespace Fan.Blogs.Data
1011
/// <summary>
1112
/// Sql implementation of the <see cref="ITagRepository"/> contract.
1213
/// </summary>
13-
public class SqlTagRepository : ITagRepository
14+
public class SqlTagRepository : EFRepository<Tag>, ITagRepository
1415
{
1516
private readonly BlogDbContext _db;
16-
public SqlTagRepository(BlogDbContext db)
17+
public SqlTagRepository(BlogDbContext db) : base(db)
1718
{
1819
_db = db;
1920
}
2021

21-
public async Task<Tag> CreateAsync(Tag tag)
22-
{
23-
await _db.Tags.AddAsync(tag);
24-
await _db.SaveChangesAsync();
25-
return tag;
26-
}
27-
2822
/// <summary>
2923
/// Deletes a tag and its associated posts.
3024
/// </summary>
@@ -56,11 +50,5 @@ from pt in p.PostTags
5650
select pt).Count(),
5751
}).ToListAsync();
5852
}
59-
60-
public async Task<Tag> UpdateAsync(Tag tag)
61-
{
62-
await _db.SaveChangesAsync();
63-
return tag;
64-
}
6553
}
6654
}

0 commit comments

Comments
 (0)