Skip to content

Commit

Permalink
Add ChurchUpdate
Browse files Browse the repository at this point in the history
  • Loading branch information
davevans committed Aug 4, 2014
1 parent 1400508 commit 4983cb9
Show file tree
Hide file tree
Showing 16 changed files with 303 additions and 8 deletions.
16 changes: 15 additions & 1 deletion src/Church/Church.Components.Core/ChurchService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Church.Common.Logging;
using System;
using Church.Common.Logging;
using Church.Common.Structures;
using Church.Common.Xml;


namespace Church.Components.Core
Expand Down Expand Up @@ -27,6 +29,18 @@ public void Add(Model.Church church)
{
throw new ErrorException("Failed to add church.", error);
}

_debug.Log("Successfully added church.{0}{1}", Environment.NewLine, church.ToXmlString());
}

public void Update(Model.Church church)
{
Error error;
if (!_churchRepository.TryUpdate(church, out error))
{
throw new ErrorException("Failed to update church.", error);
}
_debug.Log("Successfully updated church.{0}{1}", Environment.NewLine, church.ToXmlString());
}

public void Start()
Expand Down
2 changes: 2 additions & 0 deletions src/Church/Church.Components.Core/IChurchService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ public interface IChurchService : IService
{
Model.Church GetById(int churchId);
void Add(Model.Church church);
void Update(Model.Church church);

}
}
7 changes: 6 additions & 1 deletion src/Church/Church.Components.Core/Model/Church.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;

namespace Church.Components.Core.Model
{
Expand All @@ -9,6 +10,10 @@ public class Church
public TimeZone TimeZone { get; set; }
public List<Location> Locations { get; set; }

public bool IsArchived { get; set; }
public DateTime Created { get; set; }
public DateTime LastUpdated { get; set; }

public Church()
{
Locations = new List<Location>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ public interface IChurchRepository
{
Model.Church GetById(int churchId);
bool TryAdd(Model.Church church, out Error error);
bool TryUpdate(Model.Church church, out Error error);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,9 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Startup.cs" />
<Compile Include="ViewModels\AddressViewModel.cs" />
<Compile Include="ViewModels\BadRequestViewModel.cs" />
<Compile Include="ViewModels\Errors\BadRequestViewModel.cs" />
<Compile Include="ViewModels\ChurchViewModel.cs" />
<Compile Include="ViewModels\Errors\NotFoundViewModel.cs" />
<Compile Include="ViewModels\LocationViewModel.cs" />
<Compile Include="ViewModels\TimeZoneViewModel.cs" />
</ItemGroup>
Expand Down
47 changes: 47 additions & 0 deletions src/Church/Church.Host.Owin.Core/Controllers/ChurchController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
Expand All @@ -9,6 +10,7 @@
using Church.Common.Structures;
using Church.Components.Core;
using Church.Host.Owin.Core.ViewModels;
using Church.Host.Owin.Core.ViewModels.Errors;

namespace Church.Host.Owin.Core.Controllers
{
Expand Down Expand Up @@ -66,6 +68,51 @@ public HttpResponseMessage AddChurch([FromBody]ChurchViewModel churchViewModel)
}
}

[HttpPut]
[Route("api/church/{churchId}")]
public HttpResponseMessage UpdateChurch(int churchId, [FromBody] ChurchViewModel churchViewModel)
{
if (!ModelState.IsValid)
{
return Request.CreateResponse(HttpStatusCode.BadRequest, new BadRequestViewModel
{
Errors = ModelState.Values.SelectMany(e => e.Errors)
.Select(x => x.ErrorMessage)
.ToList()
});
}

var existing = _churchService.GetById(churchId);
if (existing == null)
{
return Request.CreateResponse(HttpStatusCode.NotFound, new NotFoundViewModel
{
ErrorMessage = @"Church with Id:{0} not found.".FormatWith(churchId)
});
}

try
{
churchViewModel.Id = churchId;
var church = Mapper.Map<ChurchViewModel, Components.Core.Model.Church>(churchViewModel);
_churchService.Update(church);
var viewmodel = Mapper.Map<Components.Core.Model.Church, ChurchViewModel>(church);
return Request.CreateResponse(HttpStatusCode.OK, viewmodel);
}
catch (ErrorException errorException)
{
if (errorException.Error.Code == Types.Core.ChurchErrors.DUPLICATE_CHURCH_NAME)
{
return Request.CreateResponse(HttpStatusCode.BadRequest, new BadRequestViewModel
{
Errors = new List<string> { @"The church name '{0}' already exists. Church names must be unique.".FormatWith(churchViewModel.Name) }
});
}

throw;
}
}


[HttpGet]
[Route("api/church/{churchId}/locations")]
Expand Down
9 changes: 7 additions & 2 deletions src/Church/Church.Host.Owin.Core/MappingConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ internal static void Configure()
Mapper.CreateMap<Components.Core.Model.Church, ChurchViewModel>()
.ForMember(x => x.Id, o => o.MapFrom(d => d.Id))
.ForMember(x => x.Name, o => o.MapFrom(d => d.Name))
.ForMember(x => x.TimeZone, o => o.MapFrom(d => d.TimeZone));
.ForMember(x => x.TimeZone, o => o.MapFrom(d => d.TimeZone))
.ForMember(x => x.Created, o => o.MapFrom(d => d.Created))
.ForMember(x => x.LastUpdated, o => o.MapFrom(d => d.LastUpdated));

Mapper.CreateMap<ChurchViewModel, Components.Core.Model.Church>()
.ForMember(x => x.Id, o => o.MapFrom(d => d.Id))
.ForMember(x => x.Name, o => o.MapFrom(d => d.Name))
.ForMember(x => x.TimeZone, o => o.MapFrom(d => d.TimeZone));
.ForMember(x => x.TimeZone, o => o.MapFrom(d => d.TimeZone))
.ForMember(x => x.Created, o => o.MapFrom(d => d.Created))
.ForMember(x => x.LastUpdated, o => o.MapFrom(d => d.LastUpdated))
.ForMember(x => x.IsArchived, o => o.UseValue(false));

Mapper.CreateMap<Components.Core.Model.Address, AddressViewModel>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations;
using System;
using System.ComponentModel.DataAnnotations;

namespace Church.Host.Owin.Core.ViewModels
{
Expand All @@ -11,5 +12,8 @@ public class ChurchViewModel

[Required(ErrorMessage = "TimeZone is requried.")]
public TimeZoneViewModel TimeZone { get; set; }

public DateTime Created { get; set; }
public DateTime LastUpdated { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Collections.Generic;

namespace Church.Host.Owin.Core.ViewModels
namespace Church.Host.Owin.Core.ViewModels.Errors
{
public class BadRequestViewModel
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Church.Host.Owin.Core.ViewModels.Errors
{
public class NotFoundViewModel
{
public string ErrorMessage { get; set; }
}
}
Loading

0 comments on commit 4983cb9

Please sign in to comment.