You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IInsertCustomersServise.cs 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using Karsha_Site.Application.Interfaces.Contexts;
  2. using Karsha_Site.Application.Services.Costomers.Queries.GetCustomers;
  3. using Karsha_Site.Common.Dto;
  4. using Karsha_Site.Domain.Entinies.Customers;
  5. using Microsoft.AspNetCore.Hosting;
  6. using Microsoft.AspNetCore.Http;
  7. //using Microsoft.Extensions.Hosting;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. namespace Karsha_Site.Application.Services.Costomers.Commands.InsetCustomers
  15. {
  16. public interface IInsertCustomersServise
  17. {
  18. ResultDto<ResultInsertCustomerDto> Execute(RequestInsertCustomerDto request);
  19. }
  20. public class InsertCustomersServise : IInsertCustomersServise
  21. {
  22. private readonly IHostingEnvironment _environment;
  23. private readonly IDataBaseContext _dataBaseContext;
  24. public InsertCustomersServise(IDataBaseContext context, IHostingEnvironment hostingEnvironment)
  25. {
  26. _dataBaseContext = context;
  27. _environment = hostingEnvironment;
  28. }
  29. public ResultDto<ResultInsertCustomerDto> Execute(RequestInsertCustomerDto request)
  30. {
  31. Customer customer = new Customer()
  32. {
  33. Address = request.Address,
  34. FullName = request.FullName,
  35. //Image = request.Image.FileName,
  36. Link = request.Link,
  37. };
  38. Karsha_Site.Common.UploadFiles uploads = new Karsha_Site.Common.UploadFiles(_environment);
  39. var uploadedResult = uploads.UploadFile(request.Image, $@"images\CustomerImages\");
  40. if (uploadedResult != null)
  41. {
  42. customer.Image = uploadedResult.FileNameAddress;
  43. }
  44. customer.Code = _dataBaseContext.Customers.Count() + 1;
  45. customer.InsertTime = DateTime.Now;
  46. _dataBaseContext.Customers.Add(customer);
  47. _dataBaseContext.SaveChanges();
  48. return new ResultDto<ResultInsertCustomerDto>(){
  49. Data = new ResultInsertCustomerDto()
  50. {
  51. CustomerId = customer.ID
  52. },
  53. IsSuccess = true,
  54. Message = "مشتری با موفقیت ثبت شد"
  55. };
  56. }
  57. }
  58. public class RequestInsertCustomerDto
  59. {
  60. public string FullName { get; set; }
  61. public string Address { get; set; }
  62. public string Link { get; set; }
  63. public IFormFile Image { get; set; }
  64. }
  65. public class ResultInsertCustomerDto
  66. {
  67. public int CustomerId { get; set; }
  68. }
  69. }