-
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.
Merge pull request #3 from kcg-edu-future-lab/v1.0
v1.0.7
- Loading branch information
Showing
12 changed files
with
124,211 additions
and
121 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
PostalCodes/PostalCodesWebApi/Controllers/CitiesController.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,75 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using PostalCodesWebApi.Models; | ||
|
||
namespace PostalCodesWebApi.Controllers | ||
{ | ||
/// <summary> | ||
/// 市区町村のデータを取得します。 | ||
/// </summary> | ||
[Produces("application/json")] | ||
[Route("api/[controller]")] | ||
public class CitiesController : Controller | ||
{ | ||
/// <summary> | ||
/// 都道府県コード (2 桁) を指定して、市区町村のリストを取得します。 | ||
/// </summary> | ||
/// <param name="prefectureCode">都道府県コード (2 桁)</param> | ||
/// <returns>市区町村のリスト</returns> | ||
[HttpGet("ByPrefecture/{prefectureCode:regex(^[[0-9]]{{2}}$)}")] | ||
[ProducesResponseType(200, Type = typeof(IEnumerable<City>))] | ||
[ProducesResponseType(404)] | ||
public IActionResult GetByPrefecture(string prefectureCode) | ||
{ | ||
if (!PostalCodesData.Prefectures.ContainsKey(prefectureCode)) return NotFound(); | ||
|
||
var prefecture = PostalCodesData.Prefectures[prefectureCode]; | ||
return Ok(PostalCodesData.PrefectureCitiesMap[prefecture]); | ||
} | ||
|
||
/// <summary> | ||
/// 市区町村コード (5 桁) を指定して、市区町村を取得します。 | ||
/// </summary> | ||
/// <param name="code">市区町村コード (5 桁)</param> | ||
/// <returns>市区町村</returns> | ||
[HttpGet("{code:regex(^[[0-9]]{{5}}$)}")] | ||
[ProducesResponseType(200, Type = typeof(City))] | ||
[ProducesResponseType(404)] | ||
public IActionResult Get(string code) | ||
{ | ||
if (!PostalCodesData.Cities.ContainsKey(code)) return NotFound(); | ||
|
||
return Ok(PostalCodesData.Cities[code]); | ||
} | ||
|
||
/// <summary> | ||
/// 市区町村の名前を指定して、市区町村のリストを取得します。部分一致検索です。 | ||
/// </summary> | ||
/// <param name="name">市区町村の名前</param> | ||
/// <returns>市区町村のリスト</returns> | ||
[HttpGet("ByName/{name}")] | ||
public IEnumerable<City> GetByName(string name) | ||
{ | ||
if (string.IsNullOrWhiteSpace(name)) return Enumerable.Empty<City>(); | ||
|
||
return PostalCodesData.Cities.Values.Where(x => x.Name.Contains(name)); | ||
} | ||
|
||
/// <summary> | ||
/// 市区町村のかなを指定して、市区町村のリストを取得します。部分一致検索です。 | ||
/// </summary> | ||
/// <param name="kana">市区町村のかな</param> | ||
/// <returns>市区町村のリスト</returns> | ||
[HttpGet("ByKana/{kana}")] | ||
public IEnumerable<City> GetByKana(string kana) | ||
{ | ||
if (string.IsNullOrWhiteSpace(kana)) return Enumerable.Empty<City>(); | ||
|
||
return PostalCodesData.Cities.Values.Where(x => x.Kana.Contains(kana)); | ||
} | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
PostalCodes/PostalCodesWebApi/Controllers/PostalCodesController.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,93 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using PostalCodesWebApi.Models; | ||
|
||
namespace PostalCodesWebApi.Controllers | ||
{ | ||
/// <summary> | ||
/// 郵便番号と町域のデータを取得します。 | ||
/// </summary> | ||
[Produces("application/json")] | ||
[Route("api/[controller]")] | ||
public class PostalCodesController : Controller | ||
{ | ||
/// <summary> | ||
/// 市区町村コード (5 桁) を指定して、郵便番号と町域のリストを取得します。 | ||
/// </summary> | ||
/// <param name="cityCode">市区町村コード (5 桁)</param> | ||
/// <returns>郵便番号と町域のリスト</returns> | ||
[HttpGet("ByCity/{cityCode:regex(^[[0-9]]{{5}}$)}")] | ||
[ProducesResponseType(200, Type = typeof(IEnumerable<PostalCodeEntry>))] | ||
[ProducesResponseType(404)] | ||
public IActionResult GetByCity(string cityCode) | ||
{ | ||
if (!PostalCodesData.Cities.ContainsKey(cityCode)) return NotFound(); | ||
|
||
var city = PostalCodesData.Cities[cityCode]; | ||
return Ok(PostalCodesData.CityPostalCodesMap[city]); | ||
} | ||
|
||
/// <summary> | ||
/// 郵便番号 (7 桁) を指定して、郵便番号と町域のリストを取得します。 | ||
/// </summary> | ||
/// <param name="postalCode">郵便番号 (7 桁)。ハイフンの有無は問いません。</param> | ||
/// <returns>郵便番号と町域のリスト</returns> | ||
/// <remarks>一つの郵便番号に複数の町域が割り当てられている場合があるため、戻り値はリストです。</remarks> | ||
[HttpGet("{postalCode:regex(^[[0-9]]{{3}}-?[[0-9]]{{4}}$)}")] | ||
[ProducesResponseType(200, Type = typeof(IEnumerable<PostalCodeEntry>))] | ||
[ProducesResponseType(404)] | ||
public IActionResult Get(string postalCode) | ||
{ | ||
postalCode = postalCode.Replace("-", ""); | ||
|
||
if (!PostalCodesData.PostalCodes.ContainsKey(postalCode)) return NotFound(); | ||
|
||
return Ok(PostalCodesData.PostalCodes[postalCode]); | ||
} | ||
|
||
/// <summary> | ||
/// 郵便番号の一部 (3 桁以上) を指定して、郵便番号と町域のリストを取得します。前方一致検索です。 | ||
/// </summary> | ||
/// <param name="postalCode">郵便番号の一部 (3 桁以上)。ハイフンの有無は問いません。</param> | ||
/// <returns>郵便番号と町域のリスト</returns> | ||
/// <remarks>戻り値は郵便番号の順に並びます。</remarks> | ||
[HttpGet("ByPartial/{postalCode:regex(^[[0-9]]{{3}}-?[[0-9]]{{0,4}}$)}")] | ||
public IEnumerable<PostalCodeEntry> GetByPartial(string postalCode) | ||
{ | ||
postalCode = postalCode.Replace("-", ""); | ||
|
||
return PostalCodesData.PostalCodeEntries.Where(x => x.PostalCode.StartsWith(postalCode)) | ||
.OrderBy(x => x.PostalCode); | ||
} | ||
|
||
/// <summary> | ||
/// 町域の名前を指定して、郵便番号と町域のリストを取得します。部分一致検索です。 | ||
/// </summary> | ||
/// <param name="name">町域の名前</param> | ||
/// <returns>郵便番号と町域のリスト</returns> | ||
[HttpGet("ByName/{name}")] | ||
public IEnumerable<PostalCodeEntry> GetByName(string name) | ||
{ | ||
if (string.IsNullOrWhiteSpace(name)) return Enumerable.Empty<PostalCodeEntry>(); | ||
|
||
return PostalCodesData.PostalCodeEntries.Where(x => x.TownName.Contains(name)); | ||
} | ||
|
||
/// <summary> | ||
/// 町域のかなを指定して、郵便番号と町域のリストを取得します。部分一致検索です。 | ||
/// </summary> | ||
/// <param name="kana">町域のかな</param> | ||
/// <returns>郵便番号と町域のリスト</returns> | ||
[HttpGet("ByKana/{kana}")] | ||
public IEnumerable<PostalCodeEntry> GetByKana(string kana) | ||
{ | ||
if (string.IsNullOrWhiteSpace(kana)) return Enumerable.Empty<PostalCodeEntry>(); | ||
|
||
return PostalCodesData.PostalCodeEntries.Where(x => x.TownKana.Contains(kana)); | ||
} | ||
} | ||
} |
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
70 changes: 0 additions & 70 deletions
70
PostalCodes/PostalCodesWebApi/Controllers/ValuesController.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace PostalCodesWebApi.Models | ||
{ | ||
public static class EnumerableHelper | ||
{ | ||
public static IEnumerable<IGrouping<TKey, TSource>> GroupBySequentially<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector) | ||
{ | ||
if (source == null) throw new ArgumentNullException("source"); | ||
if (keySelector == null) throw new ArgumentNullException("keySelector"); | ||
|
||
var queue = new Queue<TSource>(); | ||
var currentKey = default(TKey); | ||
var comparer = EqualityComparer<TKey>.Default; | ||
|
||
foreach (var item in source) | ||
{ | ||
var key = keySelector(item); | ||
|
||
if (!comparer.Equals(key, currentKey)) | ||
{ | ||
if (queue.Count != 0) | ||
{ | ||
yield return new Grouping<TKey, TSource>(currentKey, queue.ToArray()); | ||
queue.Clear(); | ||
} | ||
currentKey = key; | ||
} | ||
queue.Enqueue(item); | ||
} | ||
|
||
if (queue.Count != 0) | ||
{ | ||
yield return new Grouping<TKey, TSource>(currentKey, queue.ToArray()); | ||
queue.Clear(); | ||
} | ||
} | ||
} | ||
|
||
public class Grouping<TKey, TElement> : IGrouping<TKey, TElement> | ||
{ | ||
public TKey Key { get; } | ||
protected IEnumerable<TElement> Values { get; } | ||
|
||
public Grouping(TKey key, IEnumerable<TElement> values) | ||
{ | ||
Key = key; | ||
Values = values; | ||
} | ||
|
||
public IEnumerator<TElement> GetEnumerator() | ||
{ | ||
return Values.GetEnumerator(); | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() | ||
{ | ||
return GetEnumerator(); | ||
} | ||
} | ||
} |
Oops, something went wrong.