Skip to content
Open
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
477 changes: 477 additions & 0 deletions .gitignore

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions ConsumeAPI.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

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

<ItemGroup>
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="7.0.6" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

</Project>
141 changes: 141 additions & 0 deletions Controllers/ItemController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
using ConsumeAPI.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace ConsumeAPI.Controllers
{
public class ItemController : Controller
{
string baseURL = "http://localhost:5000/todos/";

[HttpGet]
public IActionResult Create()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Create(CreateItemTodo newItem)
{
using (var client = new HttpClient())
{
HttpResponseMessage response = await client.PostAsJsonAsync(baseURL, newItem);

if (response.IsSuccessStatusCode)
{
return RedirectToAction("Read", "Item");
}
else
{
Console.WriteLine("Error calling API.");
}
}
return RedirectToAction("Read", "Item");
}

[HttpGet]
public async Task<IActionResult> Read()
{
IList<ItemTodo> todoList = new List<ItemTodo>();

using (var client = new HttpClient())
{
HttpResponseMessage response = await client.GetAsync(baseURL);
if (response.IsSuccessStatusCode)
{
string dataResult = response.Content.ReadAsStringAsync().Result;
todoList = JsonConvert.DeserializeObject<IList<ItemTodo>>(dataResult);
}
else
{
Console.WriteLine("Error calling API.");
}
ViewData.Model = todoList;
}
return View();
}
[HttpGet]
public async Task<IActionResult> Details([FromRoute] Guid id)
{
ItemTodo todoItem = new ItemTodo();

using (var client = new HttpClient())
{
HttpResponseMessage getData = await client.GetAsync(baseURL+$"get?id={id}");
if (getData.IsSuccessStatusCode)
{
string dataResult = getData.Content.ReadAsStringAsync().Result;
todoItem = JsonConvert.DeserializeObject<ItemTodo>(dataResult);
}
else
{
Console.WriteLine("Error calling API.");
}
ViewData.Model = todoItem;
}
return View();
}

[HttpGet]
public async Task<IActionResult> Update([FromRoute] Guid id)
{
UpdateItemTodo updateItem = new();
using (var client = new HttpClient())
{
HttpResponseMessage response = await client.GetAsync(baseURL+$"get?id={id}");

if (response.IsSuccessStatusCode)
{
string dataResult = response.Content.ReadAsStringAsync().Result;
var item = JsonConvert.DeserializeObject<ItemTodo>(dataResult);
updateItem = new UpdateItemTodo()
{
Title = item.Title,
Description = item.Description
};
}
else
{
Console.WriteLine("Error calling API.");
}
}
return View(updateItem);
}
[HttpPost]
public async Task<IActionResult> Update(UpdateItemTodo updateItem)
{
using (var client = new HttpClient())
{
HttpResponseMessage response = await client.PutAsJsonAsync(baseURL+$"put?id={updateItem.Id}", updateItem);

if (response.IsSuccessStatusCode)
{
return RedirectToAction("Read", "Item");
}
else
{
Console.WriteLine("Error calling API.");
}
}
return RedirectToAction("Read", "Item");
}

[HttpGet]
public async Task<IActionResult> Delete([FromRoute] Guid id)
{
using (var client = new HttpClient())
{
HttpResponseMessage getData = await client.DeleteAsync(baseURL + $"delete?id={id}");
if (getData.IsSuccessStatusCode)
{
return RedirectToAction("Read", "Item");
}
else
{
Console.WriteLine("Error calling API.");
}
}
return RedirectToAction("Read", "Item");
}
}
}
43 changes: 43 additions & 0 deletions Controllers/LoginTodoController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Microsoft.AspNetCore.Mvc;
using ConsumeAPI.Models;
using System.Security.Claims;

namespace ConsumeAPI.Controllers
{
public class LoginTodoController : Controller
{
string baseURL = "http://localhost:5000/";

[HttpGet]
public IActionResult Login()
{
ClaimsPrincipal claimUser = HttpContext.User;
if (claimUser.Identity.IsAuthenticated)
{
return RedirectToAction("Read", "Item");
}
return View();
}
[HttpPost]
public async Task<IActionResult> Login(LoginItemTodo loginModel)
{
if (loginModel.Username != null && loginModel.Password != null)
{
using (var client = new HttpClient())
{
HttpResponseMessage getData = await client.PostAsJsonAsync(baseURL + "Login", loginModel);
if (getData.IsSuccessStatusCode)
{
return RedirectToAction("Read", "Item");
}
else
{
Console.WriteLine("Error calling API.");
}
}
}
ViewData["ValidateMessage"] = "User Not Found";
return View();
}
}
}
8 changes: 8 additions & 0 deletions Models/CreateItemTodo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace ConsumeAPI.Models
{
public class CreateItemTodo
{
public string Title { get; set; }
public string Description { get; set; }
}
}
9 changes: 9 additions & 0 deletions Models/ErrorViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace ConsumeAPI.Models
{
public class ErrorViewModel
{
public string? RequestId { get; set; }

public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
}
}
9 changes: 9 additions & 0 deletions Models/ItemTodo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace ConsumeAPI.Models
{
public class ItemTodo
{
public Guid Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
}
}
12 changes: 12 additions & 0 deletions Models/LoginItemTodo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.ComponentModel.DataAnnotations;

namespace ConsumeAPI.Models
{
public class LoginItemTodo
{
[Key]
public string Username { get; set; } = null!;
public string Password { get; set; } = null!;
public bool IsLoggedIn { get; set; }
}
}
9 changes: 9 additions & 0 deletions Models/UpdateItemTodo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace ConsumeAPI.Models
{
public class UpdateItemTodo
{
public Guid Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
}
}
35 changes: 35 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

using Microsoft.AspNetCore.Authentication.Cookies;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();


builder.Services.AddAuthentication(
CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(option =>
{
option.LoginPath = "/LoginTodo/Login";
option.ExpireTimeSpan = TimeSpan.FromMinutes(60);
}
);
var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();

app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();


app.MapControllerRoute(
name: "default",
pattern: "{controller=LoginTodo}/{action=Login}/{id?}");

app.Run();
28 changes: 28 additions & 0 deletions Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:1000",
"sslPort": 0
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:5144",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
21 changes: 21 additions & 0 deletions Views/Item/Create.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@*
For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
}
@model ConsumeAPI.Models.ItemTodo

<div class="container w-50 mt-5">
<h1>Add New Todo</h1>
<form action="Create" method="post">
<div class="mb-3">
<label for="exampleInputEmail1" class="form-label">Title</label>
<input type="text" class="form-control" asp-for="Title">
</div>
<div class="mb-3">
<label for="exampleInputPassword1" class="form-label">Description</label>
<textarea class="form-control" asp-for="Description"></textarea>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
39 changes: 39 additions & 0 deletions Views/Item/Details.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
@model ConsumeAPI.Models.ItemTodo
@{
ViewData["title"] = "Details | Todo";
}
<div>
<h1>Task Details</h1>
<table class="table">
<thead>
<tr>
<th>S.N.</th>
<th>ID</th>
<th>Title</th>
<th>Description</th>
<th colspan="3">Actions</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>@Model.Id</td>
<td>@Model.Title</td>
<td>@Model.Description</td>
<td>
<a asp-action="Update" asp-route-id="@Model.Id">
Update
</a>
</td>
<td>
<a asp-action="Delete" asp-route-id="@Model.Id">
Delete
</a>
</td>
</tr>
</tbody>
</table>
<a asp-action="Read">
Go Back
</a>
</div>
Loading