Beispiel #1
0
        /// <summary>
        /// Deletes a product from the DB
        /// </summary>
        /// <param name="p"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public static async Task Delete(Product p, ProductContext context)
        {
            await context.AddAsync(p);

            context.Entry(p).State = EntityState.Deleted;
            await context.SaveChangesAsync();
        }
Beispiel #2
0
        public async Task <Product> AddProductAsync(Product product)
        {
            var productResult = await _productContext.AddAsync(product);

            await _productContext.SaveChangesAsync();

            return(productResult.Entity);
        }
Beispiel #3
0
        /// <summary>
        /// Create a product to add to ProductDB
        /// </summary>
        /// <param name="p"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public static async Task <Product> Create(Product p, ProductContext context)
        {
            await context.AddAsync(p);

            await context.SaveChangesAsync();

            return(p);
        }
        public async Task <IActionResult> Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                var user = await _context.Users
                           .FirstOrDefaultAsync(u => u.Email == model.Email);

                if (user == null)
                {
                    var userRole = await _context.Roles
                                   .FirstOrDefaultAsync(u => u.Name == "user");

                    user = new User
                    {
                        FirstName      = model.FirstName,
                        MiddleName     = model.MiddleName,
                        LastName       = model.LastName,
                        Age            = model.Age,
                        Email          = model.Email,
                        Login          = model.Login,
                        Password       = model.Password,
                        Phone          = model.Phone,
                        DateRegistered = DateTime.Now,
                        Role           = userRole
                    };

                    try
                    {
                        await _context.AddAsync(user);

                        await _context.SaveChangesAsync();

                        string tempkey = TempKey();
                        Response.Cookies.Append("tempData", tempkey);

                        var callbackUrl = Url.Action(
                            "Confirmation",
                            "Account",
                            new { userId = user.ID, code = tempkey },
                            protocol: HttpContext.Request.Scheme);
                        EmailService emailService = new EmailService();
                        await emailService.SendEmailAsync(user.Email, "Подтвердите ваш аккаунт",
                                                          $"Подтвердите регистрацию на сайте \"WebStore\", перейдя по ссылке: <a href='{callbackUrl}'>link</a>");

                        return(RedirectToAction("Confirmation", "Account"));
                    }
                    catch
                    {
                        return(Content("Ошибка при сохранении."));
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Такой email уже существует");
                }
            }
            return(View(model));
        }
Beispiel #5
0
        public static async Task <Product> Edit(Product p, ProductContext context)
        {
            await context.AddAsync(p);

            context.Entry(p).State = EntityState.Modified;
            await context.SaveChangesAsync();

            return(p);
        }
        public async Task <bool> AddProduct(Product productToAdd)
        {
            await _context.AddAsync(productToAdd);

            if (await _context.SaveChangesAsync() > 0)
            {
                return(true);
            }
            return(false);
        }
        public async Task <Domain.Entities.Product.Product> AddAsync(Domain.Entities.Product.Product product)
        {
            _context.ChangeTracker.AutoDetectChangesEnabled = false;
            await _context.AddAsync(product);

            _cachingRepository.Cache(product.Id.ToString(), product);

            _context.ChangeTracker.AutoDetectChangesEnabled = true;
            return(product);
        }
Beispiel #8
0
        public async Task <IActionResult> Create(Product product)
        {
            if (ModelState.IsValid)
            {
                await _context.AddAsync(product);

                return(RedirectToAction(nameof(Index)));
            }
            return(View(product));
        }
Beispiel #9
0
        public async Task <Result> CreateProduct(Product product)
        {
            await _productContext.AddAsync <Product>(product);

            await _productContext.SaveChangesAsync();

            return(await Task.FromResult <Result>(new Result()
            {
                IsSuccess = true, Message = "Done"
            }));
        }
        public virtual async Task <T> AddAsync(T aEntity)
        {
            if (aEntity == null)
            {
                throw new NoNullAllowedException($"{nameof(AddAsync)} Entity is null");
            }
            try
            {
                await Context.AddAsync(aEntity);

                return(aEntity);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
Beispiel #11
0
        public async Task <TEntity> AddAsync(TEntity entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException($"{nameof(AddAsync)} entity must not be null");
            }
            try
            {
                await productContext.AddAsync(entity);

                await productContext.SaveChangesAsync();

                return(entity);
            }
            catch (Exception ex)
            {
                throw new Exception($"{nameof(entity)} could not be null saved:{ex.Message}");
            }
        }
        public async Task AddProductAsync(Product product)
        {
            await _product.AddAsync(product);

            await _product.SaveChangesAsync();
        }
Beispiel #13
0
        public async Task <IActionResult> Create(Product product, IFormFileCollection uploadedFiles)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }
            product.DateOfAppearances = DateTime.Now;
            string fullPath = null;

            try
            {
                #region Upload files
                if (uploadedFiles.Any())
                {
                    string[] permittedExtensions = { ".gif", ".jpeg", ".jpg", ".png" };

                    int    index      = 0;
                    var    random     = new Random();
                    string nameFolder = $"{product.Title}_{random.Next(10000, 99999)}";
                    string path       = $"/files/images/{nameFolder}";
                    fullPath = $"{_appEnvironment.WebRootPath}{path}";

                    foreach (var file in uploadedFiles)
                    {
                        if (file != null)
                        {
                            var ext = Path.GetExtension(file.FileName);

                            if (!permittedExtensions.Contains(ext.ToLowerInvariant()))
                            {
                                ModelState.AddModelError("", "Сохранить можно только изображения в формате \".gif\", \".jpeg\", \".jpg\", \".png\"");
                                if (Directory.Exists(fullPath))
                                {
                                    Directory.Delete(fullPath, true);
                                }
                                return(View(product));
                            }

                            string tempPath = path;

                            if (!Directory.Exists(fullPath))
                            {
                                Directory.CreateDirectory(fullPath);
                            }

                            tempPath += $"/{index++}_{file.FileName}";

                            using (var fileStream = new FileStream(_appEnvironment.WebRootPath + tempPath, FileMode.Create))
                            {
                                await file.CopyToAsync(fileStream);
                            }
                        }
                    }

                    product.PathToImages = path;

                    await _context.AddAsync(product);

                    await _context.SaveChangesAsync();
                }
                #endregion
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", $"Произошла непредвиденная ошибка, подробнее: {ex.Message}");
                if ((fullPath != null) && (Directory.Exists(fullPath)))
                {
                    Directory.Delete(fullPath, true);
                }
            }

            return(RedirectToAction(nameof(Index)));
        }
 public void Add(Product product)
 {
     _context.AddAsync(product);
     _context.SaveChangesAsync();
 }
Beispiel #15
0
        public async Task Insert(Product product)
        {
            await db.AddAsync(product);

            await db.SaveChangesAsync();
        }
Beispiel #16
0
        public async Task SaveProductAsync(DomainCore.Models.Product product)
        {
            await _productContext.AddAsync(product);

            await _productContext.SaveChangesAsync();
        }