Skip to content

Commit

Permalink
updated to ASPNETCore 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianGosebrink committed Sep 24, 2019
1 parent 9958676 commit af83030
Show file tree
Hide file tree
Showing 33 changed files with 12,323 additions and 12,280 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
/backend/ASPNETCore/bin/
/frontend/angularsignalrclient/.temp
backend/ASPNETCore/Properties/PublishProfiles/
/backend/ASPNETCore/.vs
/backend/ASPNETCore/ASPNETCore.Web/bin
/backend/ASPNETCore/ASPNETCore.Web/obj
18 changes: 18 additions & 0 deletions backend/ASPNETCore/ASPNETCore.Web/ASPNETCore.Web.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoMapper" Version="9.0.0" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>
9 changes: 9 additions & 0 deletions backend/ASPNETCore/ASPNETCore.Web/ASPNETCore.Web.csproj.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup>
<ActiveDebugProfile>ASPNETCore.Web</ActiveDebugProfile>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,119 +1,124 @@
using System;
using System.Collections.Generic;
using System.Linq;
using AspNetCoreAngularSignalR.Dtos;
using AspNetCoreAngularSignalR.Hubs;
using AspNetCoreAngularSignalR.Models;
using AspNetCoreAngularSignalR.Repositories;
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;

namespace AspNetCoreAngularSignalR.Controllers
{
[Route("api/[controller]")]
public class FoodItemsController : Controller
{
private readonly IFoodRepository _foodRepository;
private readonly IHubContext<CoolMessagesHub> _coolMessageHubContext;

public FoodItemsController(IFoodRepository foodRepository, IHubContext<CoolMessagesHub> hubContext)
{
_foodRepository = foodRepository;
_coolMessageHubContext = hubContext;
}

[HttpGet]
public ActionResult<List<FoodItemDto>> GetAllFoods()
{
var foods = _foodRepository.GetAll();
return Ok(foods.Select(x => Mapper.Map<FoodItemDto>(x)));
}

[HttpGet]
[Route("{id:int}", Name = nameof(GetSingleFood))]
public ActionResult<FoodItemDto> GetSingleFood(int id)
{
FoodItem foodItem = _foodRepository.GetSingle(id);

if (foodItem == null)
{
return NotFound();
}

return Ok(Mapper.Map<FoodItemDto>(foodItem));
}

[HttpPost]
public ActionResult AddFoodToList([FromBody] FoodItemDto viewModel)
{
if (viewModel == null)
{
return BadRequest();
}

if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}

FoodItem item = Mapper.Map<FoodItem>(viewModel);
item.Created = DateTime.Now;
FoodItem newFoodItem = _foodRepository.Add(item);

_coolMessageHubContext.Clients.All.SendAsync("FoodAdded",newFoodItem);

return CreatedAtRoute(
nameof(GetSingleFood),
new { id = newFoodItem.Id },
Mapper.Map<FoodItemDto>(newFoodItem));
}

[HttpPut]
[Route("{foodItemId:int}")]
public ActionResult<FoodItemDto> UpdateFoodInList(int foodItemId, [FromBody] FoodItemDto viewModel)
{
if (viewModel == null)
{
return BadRequest();
}

if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}


FoodItem singleById = _foodRepository.GetSingle(foodItemId);

if (singleById == null)
{
return NotFound();
}

singleById.ItemName = viewModel.ItemName;

FoodItem newFoodItem = _foodRepository.Update(singleById);
_coolMessageHubContext.Clients.All.SendAsync("FoodUpdated", newFoodItem);
return Ok(Mapper.Map<FoodItemDto>(newFoodItem));
}

[HttpDelete]
[Route("{foodItemId:int}")]
public ActionResult DeleteFoodFromList(int foodItemId)
{

FoodItem singleById = _foodRepository.GetSingle(foodItemId);

if (singleById == null)
{
return NotFound();
}

_foodRepository.Delete(foodItemId);

_coolMessageHubContext.Clients.All.SendAsync("FoodDeleted");
return NoContent();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using AspNetCoreAngularSignalR.Dtos;
using AspNetCoreAngularSignalR.Hubs;
using AspNetCoreAngularSignalR.Models;
using AspNetCoreAngularSignalR.Repositories;
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;

namespace AspNetCoreAngularSignalR.Controllers
{
[Route("api/[controller]")]
public class FoodItemsController : Controller
{
private readonly IFoodRepository _foodRepository;
private readonly IHubContext<ChatHub> _chatHubContext;
private readonly IMapper _mapper;

public FoodItemsController(
IFoodRepository foodRepository,
IHubContext<ChatHub> hubContext,
IMapper mapper)
{
_foodRepository = foodRepository;
_chatHubContext = hubContext;
_mapper = mapper;
}

[HttpGet]
public ActionResult<List<FoodItemDto>> GetAllFoods()
{
var foods = _foodRepository.GetAll();
return Ok(foods.Select(x => _mapper.Map<FoodItemDto>(x)));
}

[HttpGet]
[Route("{id:int}", Name = nameof(GetSingleFood))]
public ActionResult<FoodItemDto> GetSingleFood(int id)
{
FoodItem foodItem = _foodRepository.GetSingle(id);

if (foodItem == null)
{
return NotFound();
}

return Ok(_mapper.Map<FoodItemDto>(foodItem));
}

[HttpPost]
public ActionResult AddFoodToList([FromBody] FoodItemDto viewModel)
{
if (viewModel == null)
{
return BadRequest();
}

if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}

FoodItem item = _mapper.Map<FoodItem>(viewModel);
item.Created = DateTime.Now;
FoodItem newFoodItem = _foodRepository.Add(item);

_chatHubContext.Clients.All.SendAsync("FoodAdded", newFoodItem);

return CreatedAtRoute(
nameof(GetSingleFood),
new { id = newFoodItem.Id },
_mapper.Map<FoodItemDto>(newFoodItem));
}

[HttpPut]
[Route("{foodItemId:int}")]
public ActionResult<FoodItemDto> UpdateFoodInList(int foodItemId, [FromBody] FoodItemDto viewModel)
{
if (viewModel == null)
{
return BadRequest();
}

if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}


FoodItem singleById = _foodRepository.GetSingle(foodItemId);

if (singleById == null)
{
return NotFound();
}

singleById.ItemName = viewModel.ItemName;

FoodItem newFoodItem = _foodRepository.Update(singleById);
_chatHubContext.Clients.All.SendAsync("FoodUpdated", newFoodItem);
return Ok(_mapper.Map<FoodItemDto>(newFoodItem));
}

[HttpDelete]
[Route("{foodItemId:int}")]
public ActionResult DeleteFoodFromList(int foodItemId)
{

FoodItem singleById = _foodRepository.GetSingle(foodItemId);

if (singleById == null)
{
return NotFound();
}

_foodRepository.Delete(foodItemId);

_chatHubContext.Clients.All.SendAsync("FoodDeleted");
return NoContent();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;

namespace AspNetCoreAngularSignalR.Dtos
{
public class FoodItemDto
{
public int Id { get; set; }
public string ItemName { get; set; }
public DateTime Created { get; set; }
}
}
using System;

namespace AspNetCoreAngularSignalR.Dtos
{
public class FoodItemDto
{
public int Id { get; set; }
public string ItemName { get; set; }
public DateTime Created { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
using AspNetCoreAngularSignalR.Dtos;
using AspNetCoreAngularSignalR.Models;
using Microsoft.Extensions.DependencyInjection;

namespace ASPNETCore.Extensions
{
public static class MappingExtensions
{
public static void AddMappingProfiles(this IServiceCollection services)
{
AutoMapper.Mapper.Initialize(mapper =>
{
mapper.CreateMap<FoodItem, FoodItemDto>().ReverseMap();
});
}
}
}
using ASPNETCore.Web.MappingProfiles;
using AutoMapper;
using Microsoft.Extensions.DependencyInjection;

namespace ASPNETCore.Extensions
{
public static class MappingExtensions
{
public static void AddMappingProfiles(this IServiceCollection services)
{
services.AddAutoMapper(typeof(FoodMappings));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using System.Threading.Tasks;
using AspNetCoreAngularSignalR.Models;
using Microsoft.AspNetCore.SignalR;

namespace AspNetCoreAngularSignalR.Hubs
{
public class CoolMessagesHub : Hub
{
public Task SendMessage(ChatMessage chatMessage)
{
return Clients.All.SendAsync("Send", chatMessage);
}
}
}
using System.Threading.Tasks;
using AspNetCoreAngularSignalR.Models;
using Microsoft.AspNetCore.SignalR;

namespace AspNetCoreAngularSignalR.Hubs
{
public class ChatHub : Hub
{
public Task SendMessage(ChatMessage chatMessage)
{
return Clients.All.SendAsync("Send", chatMessage);
}
}
}
14 changes: 14 additions & 0 deletions backend/ASPNETCore/ASPNETCore.Web/MappingProfiles/FoodMappings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using AspNetCoreAngularSignalR.Dtos;
using AspNetCoreAngularSignalR.Models;
using AutoMapper;

namespace ASPNETCore.Web.MappingProfiles
{
public class FoodMappings : Profile
{
public FoodMappings()
{
CreateMap<FoodItem, FoodItemDto>().ReverseMap();
}
}
}
Loading

0 comments on commit af83030

Please sign in to comment.