Skip to content

Commit

Permalink
File upload
Browse files Browse the repository at this point in the history
  • Loading branch information
iracleous committed Nov 20, 2020
1 parent 9d47f3c commit aef8bf2
Show file tree
Hide file tree
Showing 21 changed files with 390 additions and 47 deletions.
1 change: 1 addition & 0 deletions Crm-Core/Services/IOrderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace ModelCrm.Services
public interface IOrderService
{
OrderOption CreateOrder(CustomerOptions customer);
OrderOption CreateOrder(int customerId);
OrderOption AddProductToOrder(int orderId, int productId);
OrderOption GetOrder(int orderId);
}
Expand Down
19 changes: 19 additions & 0 deletions Crm-Core/Services/OrderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,25 @@ public OrderOption CreateOrder(CustomerOptions customerOpt)
return orderOption;
}

public OrderOption CreateOrder(int customerId)
{

Customer customer = dbContext.Customers.Find(customerId);
if (customer == null) return null;
Order order = new Order { Customer = customer };
dbContext.Orders.Add(order);
dbContext.SaveChanges();
OrderOption orderOption = new OrderOption
{
CustomerName = customer.FirstName + " " + customer.LastName,
OrderId = order.Id
};
return orderOption;
}




public OrderOption GetOrder(int orderId)
{
Order order = dbContext.Orders.Include(o => o.Customer).FirstOrDefault(x => x.Id == orderId);
Expand Down
11 changes: 10 additions & 1 deletion Crm-Core/Services/ProductService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,16 @@ ProductOptions IProductService.CreateProduct(ProductOptions productOptions)

List<ProductOptions> IProductService.GetAllProduct()
{
List<Product> products= dbContext.Products.ToList();
List<Product> products= dbContext.Products
.Where(product => product.Quantity>5)
// .Skip(6)
// .Take(10)
.OrderByDescending (product => product.Price)
.ThenBy(product => product.Name)


.ToList();

List<ProductOptions> productOpts = new List<ProductOptions>();
products.ForEach(product => productOpts.Add(
GetProductOptionsFromProduct(product)
Expand Down
67 changes: 67 additions & 0 deletions Ms-App-Crm/Controllers/CartController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Crm_Core.Options;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using ModelCrm.Models;
using ModelCrm.Services;
using Ms_App_Crm.Models;

namespace Ms_App_Crm.Controllers
{


public class CartController : Controller
{

private readonly ILogger<CartController> _logger;
private readonly ICustomerService customerService;
private readonly IProductService productService;
private readonly IOrderService orderService;

public CartController(ILogger<CartController> logger, ICustomerService _customerService,
IProductService _productService, IOrderService _orderService)
{
_logger = logger;
customerService = _customerService;
productService = _productService;
orderService = _orderService;
}

public IActionResult Index()
{
return View();
}

public IActionResult CreateOrder()
{
int customerId = 15;

OrderOption orderOption = orderService.CreateOrder(customerId);

int orderId = orderOption.OrderId;

List<ProductOptions> productsOpts = productService.GetAllProduct();
OrderModel productModel = new OrderModel
{
products = productsOpts,
orderId = orderId
};
return View(productModel);
}

public IActionResult ViewOrders()
{

AllOrdersModel allOrdersModel = new AllOrdersModel();
//to do
//get all orders from order service

return View(allOrdersModel);
}


}
}
55 changes: 51 additions & 4 deletions Ms-App-Crm/Controllers/CustomerController.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft_Azure_Academy.Models;
using ModelCrm.Models;
using ModelCrm.Options;
using ModelCrm.Services;
Expand All @@ -14,15 +17,59 @@ namespace Microsoft_Azure_Academy.Controllers
[ApiController]
public class CustomerController : ControllerBase
{
private ICustomerService customerService ;
public CustomerController(ICustomerService customerService)
private readonly ICustomerService customerService;
private readonly IWebHostEnvironment hostingEnvironment;


public CustomerController(ICustomerService customerService, IWebHostEnvironment environment)
{
this.customerService = customerService;
hostingEnvironment = environment;
}

private string GetUniqueFileName(string fileName)
{
fileName = Path.GetFileName(fileName);
return Path.GetFileNameWithoutExtension(fileName)
+ "_"
+ Guid.NewGuid().ToString().Substring(0, 4)
+ Path.GetExtension(fileName);
}



[HttpPost]
public CustomerOptions AddCustomer(CustomerOptions customerOpt)
public CustomerOptions AddCustomer([FromForm] CustomerWithFileModel customerOptWithFileModel)
{
CustomerOptions customerOptions = customerService.CreateCustomer(customerOpt);

if (customerOptWithFileModel == null) return null;
var formFile = customerOptWithFileModel.Picture ;

var filename = customerOptWithFileModel.Picture.FileName;

if (formFile.Length > 0)
{

var filePath = Path.Combine(hostingEnvironment.WebRootPath, "uploadedimages", filename);


using (var stream = System.IO.File.Create(filePath))
{
formFile.CopyTo(stream);
}
}

CustomerOptions customerOpt = new CustomerOptions { FirstName = customerOptWithFileModel.FirstName,
LastName= customerOptWithFileModel.LastName,
Address = customerOptWithFileModel.Address,
Dob = customerOptWithFileModel.Dob, Email = customerOptWithFileModel.Email,
Phone= customerOptWithFileModel.Phone, VatNumber= customerOptWithFileModel.VatNumber
};

/// we save the picture path in the Phone field
customerOpt.Phone = filename;

CustomerOptions customerOptions = customerService.CreateCustomer(customerOpt);
return customerOptions;
}

Expand Down
1 change: 1 addition & 0 deletions Ms-App-Crm/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public HomeController(ILogger<HomeController> logger,ICustomerService _customerS

public IActionResult Index()
{
_logger.LogInformation("Hello World!");
return View();
}

Expand Down
21 changes: 21 additions & 0 deletions Ms-App-Crm/Models/AllOrdersModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Ms_App_Crm.Models
{

public class AnOrderModel
{
public int OrderId { get; set; }
public string CustomerName { get; set; }
public int CustomerId { get; set; }
public DateTime Date { get; set; }
}

public class AllOrdersModel
{
public List<AnOrderModel> orders { get; set; }
}
}
19 changes: 18 additions & 1 deletion Ms-App-Crm/Models/CustomerModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using ModelCrm.Options;
using Microsoft.AspNetCore.Http;
using ModelCrm.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand All @@ -15,4 +17,19 @@ public class CustomerOptionModel
{
public CustomerOptions customer { get; set; }
}

public class CustomerWithFileModel
{
public string Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string Email { get; set; }
public string VatNumber { get; set; }
public string Phone { get; set; }
public DateTime Dob { get; set; }

public IFormFile Picture { get; set; }
}

}
5 changes: 4 additions & 1 deletion Ms-App-Crm/Models/OrderModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Crm_Core.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand All @@ -7,5 +8,7 @@ namespace Ms_App_Crm.Models
{
public class OrderModel
{
public List<ProductOptions> products { get; set; }
public int orderId { get; set; }
}
}
1 change: 1 addition & 0 deletions Ms-App-Crm/Models/ProductModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ namespace Ms_App_Crm.Models
public class ProductModel
{
public List<ProductOptions> products { get; set; }

}
}
6 changes: 5 additions & 1 deletion Ms-App-Crm/Ms-App-Crm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
</ItemGroup>

<ItemGroup>
<Folder Include="Views\Order\" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.4" />
</ItemGroup>

<ItemGroup>
<Folder Include="wwwroot\uploadedimages\" />
</ItemGroup>

</Project>
16 changes: 16 additions & 0 deletions Ms-App-Crm/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,25 @@ public static void Main(string[] args)

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)

.ConfigureLogging(logBuilder =>
{
logBuilder.ClearProviders(); // removes all providers from LoggerFactory
logBuilder.AddConsole();
logBuilder.AddTraceSource("Information, ActivityTracing"); // Add Trace listener provider
})



.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});






}
}
36 changes: 36 additions & 0 deletions Ms-App-Crm/Views/Cart/CreateOrder.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
@model OrderModel
@{
ViewData["Title"] = "CreateOrder";
Layout = "~/Views/Shared/_OrdrerLayout.cshtml";
}

<h1>CreateOrder</h1>



<h2> Available products</h2>
<table border="1" cellpadding="10" cellspacing="0">
<tr>
<th> product Name </th>
<th> product Description </th>
<th> product Price </th>
</tr>

@{
foreach (var product in Model.products)
{
<tr>
<td><a href="#" onclick="buyProduct(@Model.orderId, @product.Id)">@product.Name</a> </td>
<td> @product.Description </td>
<td> @product.Price </td>
</tr>
}
}

</table>


<h2> My cart</h2>
<table id="myCart">

</table>
8 changes: 8 additions & 0 deletions Ms-App-Crm/Views/Cart/Index.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

@{
ViewData["Title"] = "Index";
Layout = "~/Views/Shared/_OrdrerLayout.cshtml";
}

<h1>Index</h1>

32 changes: 32 additions & 0 deletions Ms-App-Crm/Views/Cart/ViewOrders.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
@model AllOrdersModel
@{
ViewData["Title"] = "ViewOrders";
Layout = "~/Views/Shared/_OrdrerLayout.cshtml";
}

<h1>ViewOrders</h1>

<table id="ordersTable" border="1" cellpadding="10" cellspacing="0">
<tr>
<th> Order Id</th>
<th> Customer Name</th>
<th> Customer Id</th>
<th> Date</th>

</tr>

@{
foreach (var order in Model.orders)
{
<tr>
<td> @order.OrderId</td>
<td> @order.CustomerName</td>
<td> @order.CustomerId</td>
<td> @order.Date</td>
</tr>

}
}


</table>
Loading

0 comments on commit aef8bf2

Please sign in to comment.