-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
306 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace Micro.Starter.Api.Configs | ||
{ | ||
public class DatabaseConfig | ||
{ | ||
public string Host { set; get; } | ||
public int Port { set; get; } | ||
public string Name { set; get; } | ||
public string User { set; get; } | ||
public string Password { set; get; } | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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
38 changes: 38 additions & 0 deletions
38
Micro.Starter.Api/Migrations/20190828165715_InitialCreate.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
28 changes: 28 additions & 0 deletions
28
Micro.Starter.Api/Migrations/20190828165715_InitialCreate.cs
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,28 @@ | ||
using Microsoft.EntityFrameworkCore.Migrations; | ||
|
||
namespace Micro.Starter.Api.Migrations | ||
{ | ||
public partial class InitialCreate : Migration | ||
{ | ||
protected override void Up(MigrationBuilder migrationBuilder) | ||
{ | ||
migrationBuilder.CreateTable( | ||
name: "Weathers", | ||
columns: table => new | ||
{ | ||
Id = table.Column<string>(nullable: false), | ||
Temperature = table.Column<double>(nullable: false) | ||
}, | ||
constraints: table => | ||
{ | ||
table.PrimaryKey("PK_Weathers", x => x.Id); | ||
}); | ||
} | ||
|
||
protected override void Down(MigrationBuilder migrationBuilder) | ||
{ | ||
migrationBuilder.DropTable( | ||
name: "Weathers"); | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
Micro.Starter.Api/Migrations/ApplicationContextModelSnapshot.cs
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,36 @@ | ||
// <auto-generated /> | ||
using Micro.Starter.Api.Models; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Infrastructure; | ||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; | ||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; | ||
|
||
namespace Micro.Starter.Api.Migrations | ||
{ | ||
[DbContext(typeof(ApplicationContext))] | ||
partial class ApplicationContextModelSnapshot : ModelSnapshot | ||
{ | ||
protected override void BuildModel(ModelBuilder modelBuilder) | ||
{ | ||
#pragma warning disable 612, 618 | ||
modelBuilder | ||
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn) | ||
.HasAnnotation("ProductVersion", "3.0.0-preview8.19405.11") | ||
.HasAnnotation("Relational:MaxIdentifierLength", 63); | ||
|
||
modelBuilder.Entity("Micro.Starter.Api.Models.Weather", b => | ||
{ | ||
b.Property<string>("Id") | ||
.HasColumnType("text"); | ||
|
||
b.Property<double>("Temperature") | ||
.HasColumnType("double precision"); | ||
|
||
b.HasKey("Id"); | ||
|
||
b.ToTable("Weathers"); | ||
}); | ||
#pragma warning restore 612, 618 | ||
} | ||
} | ||
} |
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,41 @@ | ||
using Micro.Starter.Api.Configs; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.Options; | ||
using Npgsql; | ||
|
||
namespace Micro.Starter.Api.Models | ||
{ | ||
public class ApplicationContext : DbContext | ||
{ | ||
private readonly DatabaseConfig _db; | ||
public DbSet<Weather> Weathers { set; get; } | ||
|
||
public ApplicationContext(DbContextOptions options, IOptions<DatabaseConfig> dbOption) : base(options) | ||
{ | ||
_db = dbOption.Value; | ||
} | ||
|
||
protected ApplicationContext(IOptions<DatabaseConfig> dbOption) | ||
{ | ||
_db = dbOption.Value; | ||
} | ||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | ||
{ | ||
var connection = new NpgsqlConnectionStringBuilder | ||
{ | ||
Host = _db.Host, | ||
Port = _db.Port, | ||
Database = _db.Name, | ||
Username = _db.User, | ||
Password = _db.Password, | ||
SslMode = SslMode.Disable | ||
}; | ||
optionsBuilder.UseNpgsql(connection.ConnectionString); | ||
} | ||
|
||
protected override void OnModelCreating(ModelBuilder builder) | ||
{ | ||
base.OnModelCreating(builder); | ||
} | ||
} | ||
} |
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,8 @@ | ||
namespace Micro.Starter.Api.Models | ||
{ | ||
public class Weather | ||
{ | ||
public string Id { set; get; } | ||
public double Temperature { set; get; } | ||
} | ||
} |
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,15 @@ | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Threading.Tasks; | ||
using Micro.Starter.Api.Models; | ||
|
||
namespace Micro.Starter.Api.Repository | ||
{ | ||
public interface IWeatherRepository | ||
{ | ||
Task<IEnumerable<Weather>> GetAll(); | ||
Task<Weather> FindById(string id); | ||
Task<Weather> Create([NotNull] Weather weather); | ||
Task Delete(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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Micro.Starter.Api.Models; | ||
using Micro.Starter.Api.Uuid; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace Micro.Starter.Api.Repository | ||
{ | ||
public class WeatherRepository : IWeatherRepository | ||
{ | ||
private readonly ApplicationContext _db; | ||
private readonly IUuidService _uuid; | ||
|
||
public WeatherRepository(ApplicationContext db, IUuidService uuid) | ||
{ | ||
_db = db; | ||
_uuid = uuid; | ||
} | ||
|
||
public async Task<IEnumerable<Weather>> GetAll() | ||
{ | ||
return (await _db.Weathers.ToListAsync()).AsEnumerable(); | ||
} | ||
|
||
public Task<Weather> FindById(string id) | ||
{ | ||
return _db.Weathers.AsNoTracking().FirstOrDefaultAsync(x => x.Id == id); | ||
} | ||
|
||
public async Task<Weather> Create(Weather weather) | ||
{ | ||
weather.Id = _uuid.GenerateUuId(); | ||
var result = await _db.Weathers.AddAsync(weather); | ||
await _db.SaveChangesAsync(); | ||
return result.Entity; | ||
} | ||
|
||
public async Task Delete(string id) | ||
{ | ||
var entities = _db.Weathers.Where(w => w.Id == id); | ||
_db.Weathers.RemoveRange(entities); | ||
await _db.SaveChangesAsync(); | ||
} | ||
} | ||
} |
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,7 @@ | ||
namespace Micro.Starter.Api.Uuid | ||
{ | ||
public interface IUuidService | ||
{ | ||
string GenerateUuId(); | ||
} | ||
} |
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 Micro.Starter.Api.Uuid | ||
{ | ||
public class UuidService : IUuidService | ||
{ | ||
public string GenerateUuId() | ||
{ | ||
return System.Guid.NewGuid().ToString(); | ||
} | ||
} | ||
} |
Oops, something went wrong.