diff --git a/Lesson01/StartOfLesson/ToDo/Controllers/ToDoController.cs b/Lesson01/StartOfLesson/ToDo/Controllers/ToDoController.cs index f48166d..ca89994 100644 --- a/Lesson01/StartOfLesson/ToDo/Controllers/ToDoController.cs +++ b/Lesson01/StartOfLesson/ToDo/Controllers/ToDoController.cs @@ -2,33 +2,27 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using ToDoApp.Models; +using ToDoApp.Services; +using System.Linq; namespace ToDoApp.Controllers { public class ToDoController : Controller { - private static Dictionary status = new Dictionary - { - { 1, new Status { Id = 1, Value = "Not Started" } }, - { 2, new Status { Id = 2, Value = "In Progress" } }, - { 3, new Status { Id = 3, Value = "Done" } } - }; - - private static List list = new List - { - new ToDo { Id = 1, Title = "My First ToDo", Description = "Get the app working", Status = status[2] } - }; + // GET: ToDo public ActionResult Index() { - return View(list); + return View(Repository.list); } // GET: ToDo/Details/5 public ActionResult Details(int id) { - return View(); + var todo = Repository.GetTodoById(id); + return View(todo); + } // GET: ToDo/Create @@ -44,6 +38,9 @@ public ActionResult Create(IFormCollection collection) { try { + Repository.CreateToDo(collection); + // call the Repository with new method "CreateToDo" we will create + //Repository.CreateToDo --> missing something? // TODO: Add insert logic here return RedirectToAction(nameof(Index)); @@ -55,19 +52,21 @@ public ActionResult Create(IFormCollection collection) } // GET: ToDo/Edit/5 + [HttpGet] public ActionResult Edit(int id) { - return View(); + var todo = Repository.GetTodoById(id); + return View(todo); } // POST: ToDo/Edit/5 [HttpPost] [ValidateAntiForgeryToken] - public ActionResult Edit(int id, IFormCollection collection) + public ActionResult Edit(int id, IFormCollection collection) //(ToDo newtodo) { try { - // TODO: Add update logic here + Repository.SaveToDo(id, collection); return RedirectToAction(nameof(Index)); } @@ -80,7 +79,8 @@ public ActionResult Edit(int id, IFormCollection collection) // GET: ToDo/Delete/5 public ActionResult Delete(int id) { - return View(); + var todo = Repository.GetTodoById(id); + return View(todo); } // POST: ToDo/Delete/5 @@ -91,6 +91,10 @@ public ActionResult Delete(int id, IFormCollection collection) try { // TODO: Add delete logic here + Repository.DeleteToDo(id); + + //Repository.DeleteToDo() --> missing something? (parameter) + //all you need is id, dont pass in anything return RedirectToAction(nameof(Index)); } diff --git a/Lesson01/StartOfLesson/ToDo/Models/ToDo.cs b/Lesson01/StartOfLesson/ToDo/Models/ToDo.cs index 6b08b62..8aef82d 100644 --- a/Lesson01/StartOfLesson/ToDo/Models/ToDo.cs +++ b/Lesson01/StartOfLesson/ToDo/Models/ToDo.cs @@ -4,6 +4,7 @@ namespace ToDoApp.Models { public class ToDo { + public int Id { get; set; } public string Title { get; set; } public string Description { get; set; } diff --git a/Lesson01/StartOfLesson/ToDo/Services/Repository.cs b/Lesson01/StartOfLesson/ToDo/Services/Repository.cs new file mode 100644 index 0000000..16f9722 --- /dev/null +++ b/Lesson01/StartOfLesson/ToDo/Services/Repository.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using ToDoApp.Models; +using System.Linq; +using Microsoft.AspNetCore.Http; + +namespace ToDoApp.Services +{ + public class Repository + { + + public static Dictionary status = new Dictionary + { + { 1, new Status { Id = 1, Value = "Not Started" } }, + { 2, new Status { Id = 2, Value = "In Progress" } }, + { 3, new Status { Id = 3, Value = "Done" } } + }; + + public static List list = new List + { + new ToDo { Id = 1, Title = "My First ToDo", Description = "Get the app working", Status = status[2] } + }; + + public static ToDo GetTodoById(int id) + { + var todo = list.SingleOrDefault( t => t.Id == id); + return todo; + } + + public static ToDo SaveToDo(int id, IFormCollection collection) + { + var todo = GetTodoById(id); + todo.Id = Convert.ToInt32(collection["Id"]); + todo.Title = collection["Title"]; + todo.Description = collection["Description"]; + + return todo; + + // get the current todo based on id + //overwrite each property with vales from collection + //return saved todo + } + + public static void CreateToDo(IFormCollection collection) + { + var todo = new ToDo { Id = Convert.ToInt32(collection["Id"]), Title = collection["Title"], Description = collection["Description"], Status = status[1] }; + list.Add(todo); + + // no need to get anything from list + //create a new object of type todo and append values from collection + //add new todo to list + } + + public static void DeleteToDo(int id) + { + // find todo + var todo = GetTodoById(id); + // delete from list + list.Remove(todo); + + } + } +} diff --git a/Lesson01/StartOfLesson/ToDo/Views/ToDo/Create.cshtml b/Lesson01/StartOfLesson/ToDo/Views/ToDo/Create.cshtml new file mode 100644 index 0000000..5781fde --- /dev/null +++ b/Lesson01/StartOfLesson/ToDo/Views/ToDo/Create.cshtml @@ -0,0 +1,43 @@ +@model ToDoApp.Models.ToDo + +@{ + ViewData["Title"] = "Create"; +} + +

Create

+ +

ToDo

+
+
+
+
+
+
+ + + +
+
+ + + +
+
+ + + +
+
+ +
+
+
+
+ + + +@section Scripts { + @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} +} diff --git a/Lesson01/StartOfLesson/ToDo/Views/ToDo/Delete.cshtml b/Lesson01/StartOfLesson/ToDo/Views/ToDo/Delete.cshtml new file mode 100644 index 0000000..68d4123 --- /dev/null +++ b/Lesson01/StartOfLesson/ToDo/Views/ToDo/Delete.cshtml @@ -0,0 +1,38 @@ +@model ToDoApp.Models.ToDo + +@{ + ViewData["Title"] = "Delete"; +} + +

Delete

+ +

Are you sure you want to delete this?

+
+

ToDo

+
+
+
+ @Html.DisplayNameFor(model => model.Id) +
+
+ @Html.DisplayFor(model => model.Id) +
+
+ @Html.DisplayNameFor(model => model.Title) +
+
+ @Html.DisplayFor(model => model.Title) +
+
+ @Html.DisplayNameFor(model => model.Description) +
+
+ @Html.DisplayFor(model => model.Description) +
+
+ +
+ | + Back to List +
+
diff --git a/Lesson01/StartOfLesson/ToDo/Views/ToDo/Details.cshtml b/Lesson01/StartOfLesson/ToDo/Views/ToDo/Details.cshtml new file mode 100644 index 0000000..ecc9878 --- /dev/null +++ b/Lesson01/StartOfLesson/ToDo/Views/ToDo/Details.cshtml @@ -0,0 +1,36 @@ +@model ToDoApp.Models.ToDo + +@{ + ViewData["Title"] = "Details"; +} + +

Details

+ +
+

ToDo

+
+
+
+ @Html.DisplayNameFor(model => model.Id) +
+
+ @Html.DisplayFor(model => model.Id) +
+
+ @Html.DisplayNameFor(model => model.Title) +
+
+ @Html.DisplayFor(model => model.Title) +
+
+ @Html.DisplayNameFor(model => model.Description) +
+
+ @Html.DisplayFor(model => model.Description) +
+
+
+
+ @Html.ActionLink("Edit", "Edit", new { /* id = Model.PrimaryKey */ }) | + Back to List +
diff --git a/Lesson01/StartOfLesson/ToDo/Views/ToDo/Edit.cshtml b/Lesson01/StartOfLesson/ToDo/Views/ToDo/Edit.cshtml new file mode 100644 index 0000000..a20816e --- /dev/null +++ b/Lesson01/StartOfLesson/ToDo/Views/ToDo/Edit.cshtml @@ -0,0 +1,43 @@ +@model ToDoApp.Models.ToDo + +@{ + ViewData["Title"] = "Edit"; +} + +

Edit

+ +

ToDo

+
+
+
+
+
+
+ + + +
+
+ + + +
+
+ + + +
+
+ +
+
+
+
+ + + +@section Scripts { + @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} +} diff --git a/Lesson01/StartOfLesson/ToDo/Views/ToDo/Index.cshtml b/Lesson01/StartOfLesson/ToDo/Views/ToDo/Index.cshtml index 0e01995..e0792dc 100644 --- a/Lesson01/StartOfLesson/ToDo/Views/ToDo/Index.cshtml +++ b/Lesson01/StartOfLesson/ToDo/Views/ToDo/Index.cshtml @@ -44,9 +44,15 @@ @Html.DisplayFor(modelItem => item.Status) - @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) | - @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) | - @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ }) + @Html.ActionLink("Edit", "Edit", new { id = item.Id }) | + + + @Html.ActionLink("Details", "Details", new { id = item.Id }) | + + + @Html.ActionLink("Delete", "Delete", new { id = item.Id }) + + } diff --git a/practice/Program.cs b/practice/Program.cs new file mode 100644 index 0000000..3f7f053 --- /dev/null +++ b/practice/Program.cs @@ -0,0 +1,34 @@ +using System; + +namespace practice +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine("OMG"); + + string myNumber = "1.2345"; + string[] numParts = myNumber.Split('.'); + + string wholeNum = numParts[0]; + string decimalPart = numParts[1]; + + if (decimalPart.Length == 1) + { + decimalPart = decimalPart + "0"; + } + else if (decimalPart.Length > 2) + { + decimalPart = decimalPart.Substring(0,2); + } + else + { + decimalPart = "00"; + } + + string finalAnswer = wholeNum + "." + decimalPart; + + } + } +} diff --git a/practice/practice.csproj b/practice/practice.csproj new file mode 100644 index 0000000..21dff5c --- /dev/null +++ b/practice/practice.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp2.2 + + +