Exemplo n.º 1
0
        public async Task <Response <SupplierDto> > Create(CreateSupplierDto createEntityDto)
        {
            try
            {
                Supplier supplier = new Supplier
                {
                    Name      = createEntityDto.Name,
                    Phone     = createEntityDto.Phone,
                    Email     = createEntityDto.Email,
                    Address   = createEntityDto.Address,
                    ShopName  = createEntityDto.ShopName,
                    CreatedAt = DateTime.Now,
                    UpdatedAt = DateTime.Now
                };

                if (createEntityDto.Photo != null)
                {
                    string path = _photoAccessor.Add("suppliers", createEntityDto.Photo);
                    supplier.Photo = path;
                }

                await _context.Suppliers.AddAsync(supplier);

                await _context.SaveChangesAsync();

                var returnEntityDto = _mapper.Map <Supplier, SupplierDto>(supplier);

                return(new Response <SupplierDto>
                {
                    Data = returnEntityDto,
                    Message = "Supplier saved",
                    Time = DateTime.Now,
                    IsSuccess = true
                });
            }
            catch (Exception e)
            {
                return(new Response <SupplierDto>
                {
                    Data = null,
                    Message = e.Message,
                    Time = DateTime.Now,
                    IsSuccess = false
                });
            }
        }
Exemplo n.º 2
0
        public async Task <Response <ProductDto> > Create(CreateProductDto entityDto)
        {
            try
            {
                Product product = new Product
                {
                    Name        = entityDto.Name,
                    Code        = entityDto.Code,
                    CategoryId  = Guid.Parse(entityDto.CategoryId),
                    SupplierId  = Guid.Parse(entityDto.SupplierId),
                    BuyingDate  = entityDto.BuyingDate,
                    BuyingPrice = entityDto.BuyingPrice,
                    SelingPrice = entityDto.SelingPrice,
                    Quantity    = entityDto.Quantity,
                    CreatedAt   = DateTime.Now,
                    UpdatedAt   = DateTime.Now
                };

                //Todo : validasiyalar ucun helper method yarat
                var category = _context.Categories.Find(Guid.Parse(entityDto.CategoryId));
                if (category == null)
                {
                    return(new Response <ProductDto>
                    {
                        Data = null,
                        Message = "Category not found",
                        Time = DateTime.Now,
                        IsSuccess = false
                    });
                }
                var supplier = _context.Suppliers.Find(Guid.Parse(entityDto.SupplierId));
                if (supplier == null)
                {
                    return(new Response <ProductDto>
                    {
                        Data = null,
                        Message = "Supplier not found",
                        Time = DateTime.Now,
                        IsSuccess = false
                    });
                }



                if (entityDto.Photo != null)
                {
                    string path = _photoAccessor.Add("products", entityDto.Photo);
                    product.Photo = path;
                }

                await _context.Products.AddAsync(product);

                await _context.SaveChangesAsync();

                var returnEntityDto = _mapper.Map <Product, ProductDto>(product);

                return(new Response <ProductDto>
                {
                    Data = returnEntityDto,
                    Message = $"{product.Name} saved",
                    Time = DateTime.Now,
                    IsSuccess = true
                });
            }
            catch (Exception e)
            {
                return(new Response <ProductDto>
                {
                    Data = null,
                    Message = e.Message,
                    Time = DateTime.Now,
                    IsSuccess = false
                });
            }
        }
Exemplo n.º 3
0
        public async Task <Response <EmployeeDto> > Create(CreateEmployeeDto createEntityDto)
        {
            if (_context.Users.Any(x => x.Email == createEntityDto.Email))
            {
                throw new RestException(HttpStatusCode.BadRequest, new { Email = "Email already exists" });
            }

            if (_context.Users.Any(x => x.UserName == createEntityDto.UserName))
            {
                throw new RestException(HttpStatusCode.BadRequest, new { UserName = "******" });
            }

            try
            {
                Employee employee = new Employee
                {
                    Salary      = createEntityDto.Salary,
                    JoiningDate = createEntityDto.JoiningDate,
                    Address     = createEntityDto.Address,
                    CreatedAt   = DateTime.Now,
                    UpdatedAt   = DateTime.Now
                };
                User user = new User
                {
                    Email       = createEntityDto.Email,
                    UserName    = createEntityDto.UserName,
                    PhoneNumber = createEntityDto.Phone,
                    UserType    = "employee",
                };

                employee.User = user;


                if (createEntityDto.Photo != null)
                {
                    string path = _photoAccessor.Add("employees", createEntityDto.Photo);
                    employee.Photo = path;
                }


                IdentityResult result = await _userManager.CreateAsync(user, createEntityDto.Password);

                await _context.Employees.AddAsync(employee);

                await _context.SaveChangesAsync();

                if (!result.Succeeded)
                {
                    throw new Exception("Problem creating user");
                }



                var returnEntityDto = _mapper.Map <Employee, EmployeeDto>(employee);

                return(new Response <EmployeeDto>
                {
                    Data = returnEntityDto,
                    Message = "Employee saved",
                    Time = DateTime.Now,
                    IsSuccess = true
                });
            }
            catch (Exception e)
            {
                return(new Response <EmployeeDto>
                {
                    Data = null,
                    Message = e.StackTrace,
                    Time = DateTime.Now,
                    IsSuccess = false
                });
            }
        }