Skip to content

Commit

Permalink
Add viewModels
Browse files Browse the repository at this point in the history
  • Loading branch information
davevans committed Jul 13, 2014
1 parent 862ba2f commit 8fba57a
Show file tree
Hide file tree
Showing 22 changed files with 214 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@
<Compile Include="IChurchService.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Repository\CoreContext.cs" />
<Compile Include="Repository\ModelMappings\AddressMappings.cs" />
<Compile Include="Repository\ModelMappings\ChurchMappings.cs" />
<Compile Include="Repository\CoreRepository.cs" />
<Compile Include="Repository\ICoreRepository.cs" />
<Compile Include="Repository\ModelMappings\LocationMappings.cs" />
<Compile Include="Repository\ModelMappings\TimeZoneMappings.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
4 changes: 3 additions & 1 deletion src/Church/Church.Components.Core/Repository/CoreContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ namespace Church.Components.Core.Repository
{
public class CoreContext : BaseContext<CoreContext>
{
public DbSet<ModelCore.Church> Chuches { get; set; }
public DbSet<ModelCore.Church> Churches { get; set; }

protected override void OnModelCreating(DbModelBuilder builder)
{
builder.Configurations.Add(new ChurchMappings());
builder.Configurations.Add(new TimeZoneMappings());
builder.Configurations.Add(new LocationMappings());
builder.Configurations.Add(new AddressMappings());
}
}
}
12 changes: 9 additions & 3 deletions src/Church/Church.Components.Core/Repository/CoreRepository.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace Church.Components.Core.Repository
using System.Linq;
using System.Data.Entity;

namespace Church.Components.Core.Repository
{
public class CoreRepository : ICoreRepository
{
Expand All @@ -10,8 +13,11 @@ public CoreRepository()

public Model.Core.Church GetById(int churchId)
{
var church = _dbContext.Chuches.Find(churchId);
return church;
return _dbContext.Churches
.Include(x => x.Locations.Select(a => a.Address))
.Include(x => x.TimeZone)
.FirstOrDefault();

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Data.Entity.ModelConfiguration;
using Church.Model.Core;

namespace Church.Components.Core.Repository.ModelMappings
{
public class AddressMappings : EntityTypeConfiguration<Address>
{
public AddressMappings()
{
ToTable("Address", "Core");
HasKey(x => x.Id);
//HasRequired(x => x.Street1);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Data.Entity.ModelConfiguration;
using Church.Model.Core;

namespace Church.Components.Core.Repository.ModelMappings
{
public class LocationMappings : EntityTypeConfiguration<Location>
{
public LocationMappings()
{
ToTable("Location", "Core");
HasKey(x => x.Id);
//HasRequired(x => x.Name);

HasRequired(x => x.Address)
.WithMany()
.HasForeignKey(x => x.AddressId);
}
}
}
3 changes: 3 additions & 0 deletions src/Church/Church.Host.Core/Church.Host.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Startup.cs" />
<Compile Include="ViewModels\AddressViewModel.cs" />
<Compile Include="ViewModels\ChurchViewModel.cs" />
<Compile Include="ViewModels\LocationViewModel.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
Expand Down
38 changes: 35 additions & 3 deletions src/Church/Church.Host.Core/Controllers/ChurchController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System.Web.Http;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using Church.Components.Core;
using Church.Host.Core.ViewModels;

namespace Church.Host.Core.Controllers
{
Expand All @@ -13,9 +16,38 @@ public ChurchController(IChurchService churchService)

[HttpGet]
[Route("api/church/{id}")]
public Model.Core.Church Get(int id)
public ChurchViewModel ChurchById(int id)
{
return _churchService.GetById(id);
var church = _churchService.GetById(id);
return new ChurchViewModel
{
Id = church.Id,
Name = church.Name,
TimeZone = church.TimeZone
};
}

[HttpGet]
[Route("api/church/{churchId}/locations")]
public IEnumerable<LocationViewModel> ChurchLocationsByChurchId(int churchId)
{
var church = _churchService.GetById(churchId);
var locationViewModels = church.Locations.Select(x => new LocationViewModel
{
Id = x.Id,
Name = x.Name,
Address = new AddressViewModel
{
Street1 = x.Address.Street1,
Street2 = x.Address.Street2,
City = x.Address.City,
PostCode = x.Address.PostCode,
Country = x.Address.Country,
State = x.Address.State,
Id = x.Address.Id
}
});
return locationViewModels;
}
}
}
13 changes: 13 additions & 0 deletions src/Church/Church.Host.Core/ViewModels/AddressViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Church.Host.Core.ViewModels
{
public class AddressViewModel
{
public int Id { get; set; }
public string Street1 { get; set; }
public string Street2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string PostCode { get; set; }
public string Country { get; set; }
}
}
9 changes: 9 additions & 0 deletions src/Church/Church.Host.Core/ViewModels/ChurchViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Church.Host.Core.ViewModels
{
public class ChurchViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public Model.Core.TimeZone TimeZone { get; set; }
}
}
11 changes: 11 additions & 0 deletions src/Church/Church.Host.Core/ViewModels/LocationViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;

namespace Church.Host.Core.ViewModels
{
public class LocationViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public AddressViewModel Address { get; set; }
}
}
8 changes: 8 additions & 0 deletions src/Church/Church.Model/Church.Model.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json">
<HintPath>..\packages\Newtonsoft.Json.4.5.11\lib\net40\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
Expand All @@ -39,10 +42,15 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Core\Address.cs" />
<Compile Include="Core\Church.cs" />
<Compile Include="Core\Location.cs" />
<Compile Include="Core\TimeZone.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
13 changes: 13 additions & 0 deletions src/Church/Church.Model/Core/Address.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Church.Model.Core
{
public class Address
{
public int Id { get; set; }
public string Street1 { get; set; }
public string Street2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string PostCode { get; set; }
public string Country { get; set; }
}
}
11 changes: 10 additions & 1 deletion src/Church/Church.Model/Core/Church.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Church.Model.Core
using System.Collections.Generic;

namespace Church.Model.Core
{
public class Church
{
Expand All @@ -7,5 +9,12 @@ public class Church

public int TimeZoneId { get; set; }
public virtual TimeZone TimeZone { get; set; }

public ICollection<Location> Locations { get; set; }

public Church()
{
Locations = new List<Location>();
}
}
}
15 changes: 15 additions & 0 deletions src/Church/Church.Model/Core/Location.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Church.Model.Core
{
public class Location
{
public int Id { get; set; }
public string Name { get; set; }


public int ChurchId { get; set; }
public virtual Church Church { get; set; }

public int AddressId { get; set; }
public Address Address { get; set; }
}
}
4 changes: 4 additions & 0 deletions src/Church/Church.Model/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="4.5.11" targetFramework="net451" />
</packages>
2 changes: 2 additions & 0 deletions src/Church/Church.SQL/Church.SQL.sqlproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@
<None Include="_PostDeployment\Core.TimeZone.Populate.sql" />
<Build Include="Schemas\Core\Tables\Address.sql" />
<Build Include="Schemas\Core\Tables\Location.sql" />
<None Include="_PostDeployment\Core.Address.Populate.sql" />
<None Include="_PostDeployment\Core.Location.Populate.sql" />
</ItemGroup>
<ItemGroup>
<None Include="Church.SQL.Local.publish.xml" />
Expand Down
2 changes: 1 addition & 1 deletion src/Church/Church.SQL/Schemas/Core/Tables/Location.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
[ChurchId] INT NOT NULL,
[AddressId] INT NOT NULL,
CONSTRAINT PK_Location PRIMARY KEY (Id),
CONSTRAINT FK_Location_ChruchId FOREIGN KEY (ChurchId) REFERENCES [Core].Church(Id),
CONSTRAINT FK_Location_ChurchId FOREIGN KEY (ChurchId) REFERENCES [Core].Church(Id),
CONSTRAINT FK_Location_AddressId FOREIGN KEY (AddressId) REFERENCES [Core].[Address](Id)
)
2 changes: 2 additions & 0 deletions src/Church/Church.SQL/Script.PostDeployment.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ Post-Deployment Script Template

:r .\_PostDeployment\Core.TimeZone.Populate.sql
:r .\_PostDeployment\Core.Church.Populate.sql
:r .\_PostDeployment\Core.Address.Populate.sql
:r .\_PostDeployment\Core.Location.Populate.sql
25 changes: 25 additions & 0 deletions src/Church/Church.SQL/_PostDeployment/Core.Address.Populate.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
IF NOT EXISTS (SELECT 1 FROM Core.[Address] a WHERE a.Id = 1)
BEGIN
INSERT INTO [Core].[Address] (Street1, Street2, City, Postcode, Country)
VALUES
(
'55 Erskineville Rd',
'Erskineville',
'Sydney',
'2043',
'Australia'
)
END

IF NOT EXISTS (SELECT 1 FROM Core.[Address] a WHERE a.Id = 2)
BEGIN
INSERT INTO [Core].[Address] (Street1, Street2, City, Postcode, Country)
VALUES
(
'189 Church St',
'Newtown',
'Sydney',
'2042',
'Australia'
)
END
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
BEGIN
INSERT INTO [Core].Church (Name, TimeZoneId)
VALUES
('Erko', 85)
('NEAC', 85)
END
13 changes: 13 additions & 0 deletions src/Church/Church.SQL/_PostDeployment/Core.Location.Populate.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
IF NOT EXISTS (SELECT 1 FROM Core.Location WHERE Id = 1)
BEGIN
INSERT INTO Core.[Location] (Name, ChurchId, AddressId)
VALUES
('55 Erskineville Rd', 1,1)
END

IF NOT EXISTS (SELECT 1 FROM Core.Location WHERE Id = 2)
BEGIN
INSERT INTO Core.[Location] (Name, ChurchId, AddressId)
VALUES
('189 Church St', 1,2)
END
1 change: 1 addition & 0 deletions src/Church/packages/repositories.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
<repository path="..\Church.Common\packages.config" />
<repository path="..\Church.Components.Core\packages.config" />
<repository path="..\Church.Host.Core\packages.config" />
<repository path="..\Church.Model\packages.config" />
</repositories>

0 comments on commit 8fba57a

Please sign in to comment.