Exemplo n.º 1
0
        public async Task <IActionResult> Create(Product product)
        {
            product.Images = new List <ProductImage>();

            for (int i = 0; i < product.file.Length; i++)
            {
                var    ext      = Path.GetExtension(product.file[i].FileName);
                string purePath = $"{Guid.NewGuid()}{ext}";

                string fileName = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "Uploads", purePath);

                using (var fs = new FileStream(fileName, FileMode.CreateNew, FileAccess.Write))
                {
                    product.file[i].CopyTo(fs);
                }
                product.Images.Add(new ProductImage
                {
                    Path   = purePath,
                    IsMain = i == product.fileSelectedIndex
                });
            }

            if (ModelState.IsValid)
            {
                _context.Add(product);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CategoryId"] = new SelectList(_context.Categories, "Id", "Name", product.CategoryId);
            return(View(product));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Create([Bind("Id,Title,Body,Image,PostDate")] Post post, IFormFile myFile)
        {
            if (myFile == null)
            {
                ModelState.AddModelError("Image", "Fayl uygun deyil");
            }
            if (ModelState.IsValid)
            {
                string extension = Path.GetExtension(myFile.FileName);
                post.Image = $"{DateTime.Now:yyyyMMddHHmmss} - {Guid.NewGuid()}{extension}";

                string phisicalPath = Path.Combine(env.ContentRootPath,
                                                   "wwwroot",
                                                   "uploads",
                                                   "images",
                                                   post.Image);

                using (FileStream fs = new FileStream(phisicalPath, FileMode.Create, FileAccess.Write))
                {
                    await myFile.CopyToAsync(fs);
                }
                db.Add(post);
                await db.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(post));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Create([Bind("Id,Name,DeletedDate")] Category category)
        {
            if (ModelState.IsValid)
            {
                _context.Add(category);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(category));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Create([Bind("Id,Email")] Subscribe subscribe)
        {
            if (ModelState.IsValid)
            {
                _context.Add(subscribe);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(subscribe));
        }
Exemplo n.º 5
0
        public async Task <IActionResult> Answer(int id, [Bind("Id,Answer")] Contact contact)
        {
            try
            {
                if (id != contact.Id)
                {
                    return(NotFound());
                }

                var entity = await _context.Contacts
                             .FirstOrDefaultAsync(m => m.Id == id);

                if (entity == null)
                {
                    return(NotFound());
                }
                if (entity.Answer != null)
                {
                    return(BadRequest());
                }
                entity.Answer     = contact.Answer;
                entity.AnswerDate = DateTime.UtcNow.AddHours(4);

                var host     = config.GetValue <string>("emailAccount:smtpServer");
                var port     = config.GetValue <int>("emailAccount:smtpPort");
                var userName = config.GetValue <string>("emailAccount:userName");
                var password = config.GetValue <string>("emailAccount:password");
                var cc       = config.GetValue <string>("emailAccount:cc")
                               .Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);


                SmtpClient client = new SmtpClient(host, port);
                client.Credentials = new NetworkCredential(userName, password);
                client.EnableSsl   = true;

                MailMessage message = new MailMessage(userName, entity.Email);

                foreach (var item in cc)
                {
                    message.CC.Add(item);
                }

                message.Subject    = "Organi Musteri Servisi";
                message.Body       = entity.Answer;
                message.IsBodyHtml = true;

                client.Send(message);

                _context.Entry(entity).State = EntityState.Modified;
                await _context.SaveChangesAsync();

                TempData["Message"] = "Cavablandirildi";

                return(RedirectToAction(nameof(Index)));
            }
            catch (Exception ex)
            {
                throw;
            }
        }