public async Task <IActionResult> Edit(int id, [Bind("Id,UserName,NameLocomotive,BaseInfo,AllInfo")] UserLocomotivePhotos userLocomotivePhotos)
        {
            if (id != userLocomotivePhotos.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    userLocomotivePhotos.DateTime = DateTime.Now;
                    _context.Update(userLocomotivePhotos);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!UserLocomotivePhotosExists(userLocomotivePhotos.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(IndexAll)));
            }
            return(View(userLocomotivePhotos));
        }
        public async Task <IActionResult> AddImage(int?id, IFormFile uploads)
        {
            if (id != null)
            {
                if (uploads != null)
                {
                    UserLocomotivePhotos userLocomotive = await _context.UserLocomotivePhotos.Where(x => x.Id == id).FirstOrDefaultAsync();

                    byte[] p1 = null;
                    using (var fs1 = uploads.OpenReadStream())
                        using (var ms1 = new MemoryStream())
                        {
                            fs1.CopyTo(ms1);
                            p1 = ms1.ToArray();
                        }
                    userLocomotive.ImageMimeTypeOfData = uploads.ContentType;
                    userLocomotive.Image = p1;
                    _context.UserLocomotivePhotos.Update(userLocomotive);
                    _context.SaveChanges();
                    return(RedirectToAction(nameof(IndexAll)));
                }
            }

            return(RedirectToAction(nameof(IndexAll)));
        }
        public async Task <IActionResult> Create([Bind("Id,UserName,NameLocomotive,BaseInfo,AllInfo")] UserLocomotivePhotos userLocomotivePhotos)
        {
            string username       = "";
            int    userid         = 0;
            string email          = "";
            var    remoteIpAddres = Request.HttpContext.Connection.RemoteIpAddress.ToString();
            Users  user           = _context.User.Where(x => x.IpAddress.Contains(remoteIpAddres)).FirstOrDefault();

            if (user != null && user.Status == "true")
            {
                ViewBag.user = user;
                username     = user.Name;
                userid       = user.Id;
                email        = user.Email;
            }
            userLocomotivePhotos.UserName = username;
            userLocomotivePhotos.UserId   = userid;
            userLocomotivePhotos.Email    = email;
            userLocomotivePhotos.DateTime = DateTime.Now;
            _context.Add(userLocomotivePhotos);
            await _context.SaveChangesAsync();

            SendMessage(userLocomotivePhotos);
            UserLocomotivePhotos userLocomotiveAdded = _context.UserLocomotivePhotos.ToList().LastOrDefault();

            TempData["LocomotiveID"] = userLocomotiveAdded.Id;
            return(RedirectToAction(nameof(AddImageForm)));
        }
        public FileContentResult GetImage(int id)
        {
            UserLocomotivePhotos userLocomotive = _context.UserLocomotivePhotos
                                                  .FirstOrDefault(g => g.Id == id);

            if (userLocomotive != null)
            {
                var file = File(userLocomotive.Image, userLocomotive.ImageMimeTypeOfData);
                return(file);
            }
            else
            {
                return(null);
            }
        }
 private void SendMessage(UserLocomotivePhotos userLocomotivePhotos)
 {
     try
     {
         MailMessage m = new MailMessage("*****@*****.**", userLocomotivePhotos.Email);
         m.Body = userLocomotivePhotos.UserName + " Ваша публикация опубликована, Спасибо Вам";
         SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
         smtp.UseDefaultCredentials = true;
         smtp.Credentials           = new NetworkCredential("*****@*****.**", "SashaVinichuk");
         smtp.EnableSsl             = true;
         smtp.Send(m);
     }catch (Exception exp)
     {
         Trace.WriteLine(exp.ToString());
         string     expstr        = exp.ToString();
         FileStream fileStreamLog = new FileStream(@"Mail.log", FileMode.Append);
         for (int i = 0; i < expstr.Length; i++)
         {
             byte[] array = Encoding.Default.GetBytes(expstr.ToString());
             fileStreamLog.Write(array, 0, array.Length);
         }
         fileStreamLog.Close();
     }
 }