Beispiel #1
0
        public async Task <IActionResult> Details(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var message = await _context.Messages
                          .Include(m => m.MessageSender)
                          .ThenInclude(ms => ms.User)
                          .FirstOrDefaultAsync(m => m.Id == id);

            if (message == null)
            {
                return(NotFound());
            }

            if (message.IsRead == false)
            {
                message.IsRead = true;
                _context.Update(message);
                await _context.SaveChangesAsync();
            }

            return(View(message));
        }
Beispiel #2
0
        public async Task <IActionResult> Register([Bind("FirstName,LastName,Email,Password")] User user)
        {
            bool userExists = _context.Users.Any(u => u.Email.Equals(user.Email, StringComparison.InvariantCultureIgnoreCase));

            if (userExists)
            {
                ViewData.Add("error", "E-postadressen existerar redan!");
                return(View(user));
            }

            if (ModelState.IsValid)
            {
                string hashedPassword = BCrypt.Net.BCrypt.HashPassword(user.Password);
                user.Password = hashedPassword;

                _context.Add(user);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Login)));
            }
            return(View(user));
        }
        public async Task <IActionResult> Create([Bind("Id,Name,Title,Price,Description,Phone,CreatedDate,County,Area,CategoryIds")] Advertisement advertisement)
        {
            if (ModelState.IsValid)
            {
                //Handle image upload
                IFormFileCollection files = this.Request.Form.Files;

                var identity = (ClaimsIdentity)User.Identity;
                int userId   = Convert.ToInt32(identity.FindFirst(Models.User.UserId).Value);
                advertisement.UserId = userId;
                _context.Add(advertisement);
                await _context.SaveChangesAsync();

                foreach (int id in advertisement.CategoryIds)
                {
                    _context.AdvertisementCategories.Add(new AdvertisementCategory(advertisement.Id, id));
                }
                await _context.SaveChangesAsync();

                SaveFileUploads(files, advertisement.Id);
                return(RedirectToAction(nameof(Index)));
            }
            return(View(advertisement));
        }