Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using IResultImplementation.Data;
using IResultImplementation.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace IResultImplementation
{
public static class ContactsHandler
{

// GET: api/Contacts
public static IResult GetContacts(IResultImplementationContext context)
{
return TypedResults.Ok(context.Contact.ToList());
}

// GET: api/Contacts/5
[HttpGet("{id}")]
public static IResult GetContact(IResultImplementationContext context, int id)
{
var contact = context.Contact.Where(c => c.Id == id).FirstOrDefault();

if (contact == null)
{
return TypedResults.NotFound();
}

return TypedResults.Ok(contact);
}


// POST: api/Contacts
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPost]
public static IResult PostContact(IResultImplementationContext context, Contact contact)
{
context.Contact.Add(contact);
context.SaveChanges();

return TypedResults.CreatedAtRoute<Contact>(contact, nameof(ContactsHandler.GetContact));
}

// PUT: api/Contacts/5
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPut("{id}")]
public static IResult PutContact(IResultImplementationContext context, int id, Contact contact)
{
if (id != contact.Id)
{
return TypedResults.BadRequest();
}

context.Entry(contact).State = Microsoft.EntityFrameworkCore.EntityState.Modified;

try
{
context.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!ContactExists(context, id))
{
return TypedResults.NotFound();
}
else
{
throw;
}
}

return TypedResults.NoContent();
}


// DELETE: api/Contacts/5
[HttpDelete("{id}")]
public static IResult DeleteContact(IResultImplementationContext context, int id)
{
if (context.Contact == null)
{
return TypedResults.NotFound();
}
var contact = context.Contact.Find(id);
if (contact == null)
{
return TypedResults.NotFound();
}

context.Contact.Remove(contact);
context.SaveChanges();

return TypedResults.NoContent();
}

private static bool ContactExists(IResultImplementationContext context, int id)
{
return (context.Contact?.Any(e => e.Id == id)).GetValueOrDefault();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using IResultImplementation.Models;
using Microsoft.EntityFrameworkCore;

namespace IResultImplementation.Data
{
public class IResultImplementationContext : DbContext
{
public IResultImplementationContext(DbContextOptions<IResultImplementationContext> options)
: base(options)
{
}
public IResultImplementationContext()
{
}


public virtual DbSet<Contact> Contact { get; set; } = default!;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0-preview-20221221-03" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="7.0.3" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="MSTest.TestAdapter" Version="3.0.2" />
<PackageReference Include="MSTest.TestFramework" Version="3.0.2" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace IResultImplementation.Models
{
public class Contact
{
public int Id { get; set; }

public string Name { get; set; } = String.Empty;
public string Email { get; set; } = String.Empty;
public string PhoneNumber { get; set; } = String.Empty;
}
}
23 changes: 23 additions & 0 deletions fundamentals/minimal-apis/samples/IResultImplementation/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using IResultImplementation;
using IResultImplementation.Data;
using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<IResultImplementationContext>(options =>
options.UseInMemoryDatabase("Contacts"));
// Add services to the container.
builder.Services.AddControllers();
var app = builder.Build();

// Configure the HTTP request pipeline.

app.UseHttpsRedirection();
app.MapControllers();

app.MapGet("/api/contacts", ContactsHandler.GetContacts);
app.MapGet("/api/contacts/{id}", ContactsHandler.GetContact);
app.MapPost("/api/contacts", ContactsHandler.PostContact);
app.MapPut("/api/contacts/{id}", ContactsHandler.PutContact);
app.MapDelete("/api/contacts/{id}", ContactsHandler.DeleteContact);

app.Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using IResultImplementation.Data;
using IResultImplementation.Models;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.EntityFrameworkCore;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace IResultImplementation.Tests
{
[TestClass]
public class IResultImplementationTest
{
public IQueryable<Contact>? Data { get; set; }
public Mock<DbSet<Contact>>? MockSet { get; set; }
[TestInitialize]
public void Setup()
{
Data = new List<Contact>()
{
new Contact() { Id = 1, Name = "John", Email = "john@tmail.com", PhoneNumber = "1234567890" },
new Contact() { Id = 2, Name = "Jane", Email = "Jane@kam.com", PhoneNumber = "29384736273" },
new Contact() {Id = 3, Name = "Kahn", Email = "ema@email.com", PhoneNumber = "23239029202"}
}.AsQueryable();

MockSet = new Mock<DbSet<Contact>>();
MockSet.As<IQueryable<Contact>>().Setup(m => m.Provider).Returns(Data.Provider);
MockSet.As<IQueryable<Contact>>().Setup(m => m.Expression).Returns(Data.Expression);
MockSet.As<IQueryable<Contact>>().Setup(m => m.ElementType).Returns(Data.ElementType);
MockSet.As<IQueryable<Contact>>().Setup(m => m.GetEnumerator()).Returns(() => Data.GetEnumerator());
}
[TestMethod]
public void GetContactsReturnsContactsFromDatabase()
{
//Arrange
var mockContext = new Mock<IResultImplementationContext>();
mockContext.Setup(c => c.Contact).Returns(MockSet!.Object);
int expectedStatusCode = 200;
int expectedItemCount = 3;

//Act
var result = (Ok<List<Contact>>)ContactsHandler.GetContacts(mockContext.Object);

//Assert
Assert.AreEqual(expectedStatusCode, result.StatusCode);
Assert.AreEqual(expectedItemCount, result.Value?.Count);

}

[TestMethod]
public void GetContactReturnsAContactFromDatabase()
{
//Arrange
var mockContext = new Mock<IResultImplementationContext>();
mockContext.Setup(c => c.Contact).Returns(MockSet!.Object);
int expectedStatusCode = 200;
int expectedUserId = 2;

//Act
var result = (Ok<Contact>)ContactsHandler.GetContact(mockContext.Object, expectedUserId);

//Assert
Assert.AreEqual(expectedStatusCode, result.StatusCode);
Assert.AreEqual(expectedUserId, result.Value?.Id);

}

[TestMethod]
public void GetContactReturnsNotFound()
{
//Arrange
var mockContext = new Mock<IResultImplementationContext>();
mockContext.Setup(c => c.Contact).Returns(MockSet!.Object);
int expectedStatusCode = 404;
int expectedUserId = 20;

//Act
var result = (NotFound)ContactsHandler.GetContact(mockContext.Object, expectedUserId);

//Assert
Assert.AreEqual(expectedStatusCode, result.StatusCode);

}

[TestMethod]
public void CreateTodoSavesContactToDatabase()
{
//Arrange
var mockDbContextOptions = new Mock<DbContextOptions<IResultImplementationContext>>();
var mockContext = new Mock<IResultImplementationContext>();
mockContext.Setup(c => c.Contact).Returns(MockSet!.Object);
mockContext.Setup(c => c.Contact.Add(It.IsAny<Contact>())).Callback<Contact>(contact => Data = Data.Append(contact));
var newContact = new Contact()
{
Id = 4,
Name = "John Doe",
Email = "akd@omail.com",
PhoneNumber = "1234567890"
};
int expectedStatusCode = 201;
int expectedItemCount = 4;

//Act
var result = (CreatedAtRoute<Contact>)ContactsHandler.PostContact(mockContext.Object, newContact);

//Assert
Assert.AreEqual(newContact, result.Value);
Assert.AreEqual(expectedStatusCode, result.StatusCode);
Assert.AreEqual(expectedItemCount, Data.Count());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}