You are on page 1of 20

Name : Talha

Class : BSCS (7TH Semester)


Roll number : BCS19-024
Course : EAD

________________________________________________________________

FINAL REPORT

 Task number 01

Create a model for your JSON data source.

Solution:
We first have to create a new class named Medicines in the Model folder.
Then we will create different properties and attributes according to the
requirements of JSON data file.
Here is the code for Model of Web application.

using System.Text.Json;
using System.Text.Json.Serialization;
namespace MedicineDeliveryWebApp.Models
{
public class Medicines
{
[JsonPropertyName(“id”)]

public int medicineId { get; set; }

[JsonPropertyName(“brand”)]

public int medicinebrand { get; set; }

[JsonPropertyName(“Formula”)]

public int medicineformula { get; set; }

[JsonPropertyName(“Category”)]

public int medicinecategory { get; set; }

public override string ToString()

return JsonSerializer.Serialize<Medicines>(this);

}
 Task number 02

Create relations between Entities for your web application project.

Solution:

To create entity relations, we have to create necessary migrations and

added them to the database.

[ForeignKey(“MedicinesSeller”)]

public int SellerID { get; set; }

public virtual Seller MedicinesSeller { get; set; }

Then we have to create or made some amendments to the Medicines

Model File and have to added the following code. Also , we have to create a

Model file or entity named Seller.

using System.ComponentModel.DtaAnnotations;

namespace MedicinesDeliveryWebApp.Models

{
public class Seller

[Key]

public int ID { get; set; }

[Required]

public string Name { get; set; }

 Task number 03

Create a Service Component For your JSON structure.

Solution:

namespace MedicinesDeliveryWebApp.Services

public class JsonMedicinesFile


{

public JsonMedicinesFile (IWebHostEnvironment webHostEnvironment)

WebHostEnvironment = webHostEnvironment;

public IWebHostEnvironment WebHostEnvironment { get; }

public string JsonFilePath

get

return Path.Combine (WebHostEnvironment.WebRootPath, "data",

"medicines.json");

public IEnumerable<Medicines> getMedicinesData()

using (var jsonFile = File.OpenText (JsonFilePath))

return JsonSerializer.Deserialize<Medicine[]>(jsonFile.ReadToEnd());

}
 Task number 04

Create the View Component for your Model.

Solution:

First, we added the following code to the index.

<h1 class="display-4">Welcome</h1>

<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building

Web apps with ASP.NET Core</a>.</p>

</div>

<ul>

@foreach (var medicines in Model.medicines){

</ul>

<li>@medicine.medicineBrand</li>
Then we have to made some amendments to Index Model code as well as

Service code as follows

public class MedicineFile

public class JsonFileMedicineService

public JsonMedicineFile(IWebHostEnvironment webHostEnvironment)

public

JsonFileMedicineService(IWebHostEnvironment webHostEnvironment)

WebHostEnvironment webHostEnvironment;

e -19,11 +19,11 @@ public string JsonFilePath

get {

}
return Path.Combine(WebHostEnvironment.WebRootPath, "data",

"medicines.json"); return Path.Combine (WebHostEnvironment.WebRootPath,

"data", "Medicines.json");

public IEnumerable<Medicines> getMedicinesData()

public IEnumerable<Medicines> getMedicinesRecord()

 Task number 05

Create an Application Programming Interface (API) for your

web application that gives a list of items data that are stored in

JSON format.

Solution:

We have to create a new class for creating an Application Programming Interface


controller and wrote necessary code to it and also made some amendments or
changes to the start-up file.
using MedicinesDeliveryWebApp.Models; using
MedicinesDeliveryWebApp.Services; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions. DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
namespace MedicinesDeliveryWebApp
@@ -26,6 +29,7 @@ public void ConfigureServices (IServiceCollection
services)
{
}
services.AddRazorPages();
services.AddTransient <JsonFileMedicinesService>();
services.AddControllers();
// This method gets called by the runtime. Use this method to configure the
HTTP request pipeline.
@@ -52,6 +56,7 @@ public void Configure(IApplicationBuilder app,
IwebHostEnvironment env)
app.UseEndpoints(endpoints =>
{
});
endpoints.MapRazorPages();
endpoints.MapControllers();

 Task number 06

Create a StyleSheet Application Programming Interface for


your Project that return the CSS code in String Format.

Solution:

We created a new class named for the stylesheet API controller


and wrote necessary code to it and also created a service file for that
purpose.

using MedicinesDeliveryWebApp.Models;

using MedicinesDeliveryWebApp.Services; + using


Microsoft.AspNetCore.Http; + using Microsoft.AspNetCore.Mvc;
+ using System.Collections.Generic;

namespace MedicinesDeliveryWebApp.Controllers

[Route("api/[controller]")]
[ApiController]

public class StyleSheetController: ControllerBase

cssFileStyleSheetService styleSheet Service;

public StyleSheetController (cssFileStyleSheet Service


styleSheetService)

this.stylesheetService styleSheet Service;

[HttpGet]

public string Get()

return stylesheetService.getStyleSheet();

using ComputerShoppingWebApp.Models;

using Microsoft.AspNetCore.Hosting; using System.IO;

using System.Text.Json;

namespace ComputerShoppingWebApp.Services

public class cssFileStyleSheetService


public cssFileStyleSheetService(IWebHostEnvironment
webHostEnvironment)

}
WebHostEnvironment webHostEnvironment;

public WebHostEnvironment WebHostEnvironment { get; }

public string cssFilePath

get
{
return Path.Combine(WebHostEnvironment.WebRootPath, "css",
"StyleSheet.css");

}
public string getStylesheet()
{

using (var cssFile File.OpenText (cssFilePath))

return cssFile.ReadToEnd();

 Task number 07

Create the input form and form handler for your data.
Solution:

We created the controller for the form handler and view for input form,
and wrote the following code:

<div class="text-center">

<h1 class="display-4">welcome</h1>

<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building


web apps with ASP.NET Core</a>.</p>

<--<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building


web apps with ASP.NET Core</a>.</p>-->

</div>

+ <form action="/FormHandler" method="get">

<label for="id">Enter ID: </label>

<input id="id" type="number" name="id" value="" required />

<br />

<label for="brand">Enter Brand: </label>


<input id="brand" type="text" name="brand" value="" required /> <br />

<label for="model">Enter Model: </label>

<input id="model" type="text" name="model" value="" required /> <br />

<label for="cat">Enter Category: </label>

<input id="cat" type="text" name="category" value="" required /> <br />

<label for="price">Enter Price: </label>

<input id="price" type="text" name="price" value="" required /> <br />

<label for="ing">Enter Image URL: </label>

<input id="img" type="text" name="url" value="" required /> <br />

<input type="submit" name="submit" value="Submit" /> </form>

 Task number 08
Create the Search Form for your web application project.
Solution:

We made some changes to the Medicines Controller and created a new


view for search form. Here is the code for it:

public IActionResult SearchForm()


{
return View();
}
public async Task<IActionResult> SearchResult(string model)
{
return View("Index", await _context.Computer.Where(item-
>item.Model.Contains(model)).ToListAsync());

<h4>Book Search</h4>

<hr />

<div class="row">

<div class="col-md-4">

<form asp-action="SearchResult">
<div asp-validation-summary="ModelOnly" class="text-danger"></div> <div
class="form-group">

<label for="Model" class="control-label"></label>

<input name="Model" class="form-control" />

</div>

<div class="form-group">

<input type="submit" value="Search" class="btn btn-primary" />

</div>

</form>

</div>

</div>

 Task number 09
Add user authentication to your project.

Solution:

To add user authentication, we first added “[Authorize]” at the places


where we needed authorization.
Then we made some changes to the Medicines List file as follows:

@model IEnumerable<MedicinesDeliveryWebApp.Models.Medicines>

ViewData["Title"] = "Medicines List";

<div class="text-center">

<h1 class="display-4">Medcines List</h1>

</div>

<div class="row">

@foreach (var medicines in Model)

{
<div class="col-lg-3 col-md-6 col-sm-12′′ style="margin-bottom: 78px;">

<img src="@computer.URL" alt="@medicines.Brand @medicines.Model"


style="width:90%; height:75%; display:block; margin:0 auto;" />

<span style="display:block; text-align:center; font-size:20px;


color:black">@medicines.Brand @cmedicines.Formula</span>

<span style="display:block; text-align:center; font-size:16px; color:grey">Rs.


@computer.Price</span>

<span style="display:block; width:90%; margin:0 auto"><input


style="display:inline; width:set; text-align:center" type="number"
name="quantity" value="" placeholder="0" /><button_style="display:inline;
width:set">Add to cart</button></span>

</div>

</div>

Also made some additional changes in different files where authentication was
needed.
For example, in the details file we made the following changes:
@model

@using

MedicinesDeliveryWebApp.Models.Medicines

Microsoft.AspNetCore.Identity

@inject SignInManager<IdentityUser> SignInManager

@inject UserManager<IdentityUser> UserManager

ViewData["Title"] = "Details"
@@ -43,6 +47,9 @e
</dl>
</div>
<div>
<a asp-action="Edit" asp-route-id="@Model.ID">Edit</a> | @if
(SignInManager.IsSignedIn(User)) {
}
<a asp-action="Edit" asp-route-id="@Model.ID">Edit</a> <br />
<a asp-action="Index">Back to List</a> </div>

THANK YOU
__________________________________________________
__________

You might also like