コード例 #1
0
        public async Task <IActionResult> Profile(UserProfileViewModel model)
        {
            if (!User.Identity.IsAuthenticated)
            {
                return(BadRequest("User not logged in"));
            }

            try
            {
                if (ModelState.IsValid)
                {
                    var userToUpdate = await _userManager.GetUserAsync(User);

                    userToUpdate.FirstName = model.FirstName;
                    userToUpdate.LastName  = model.LastName;

                    _repository.UpdateEntity(userToUpdate);

                    if (_repository.SaveAll())
                    {
                        return(RedirectToAction("Profile"));
                    }
                    else
                    {
                        _logger.LogError("Could not Save for some reason");
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("Could not update user profile", ex);
            }

            return(View(model));
        }
コード例 #2
0
        public async Task <IActionResult> Create(AdvertisementCreationModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    int.TryParse(model.CategoryId, out int categoryId);

                    //check category
                    var category = _repository.GetCategory(categoryId);

                    if (User.Identity.IsAuthenticated)
                    {
                        // get logged in user
                        var user = await _userManager.GetUserAsync(User);

                        // create new advertisement
                        var advertisement = new Advertisement()
                        {
                            Title       = model.Title,
                            Description = model.Description,
                            Category    = category,
                            User        = user
                        };

                        // Add new advertisement
                        _repository.AddEntity(advertisement);

                        //save and redirect to created advertisement detail page
                        if (_repository.SaveAll())
                        {
                            return(RedirectToAction("Details", new { id = advertisement.AdvertisementId }));
                        }
                    }
                    else
                    {
                        // user is not logged in so return badrequest
                        return(BadRequest());
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("Failed to create a new advertisement: ", ex);
            }

            return(View(model));

            //if (ModelState.IsValid)
            //{
            //    _context.Add(advertisement);
            //    await _context.SaveChangesAsync();
            //    return RedirectToAction(nameof(Index));
            //}
            //ViewData["CategoryId"] = new SelectList(_context.Categories, "CategoryId", "CategoryId", advertisement.CategoryId);
            //return View(advertisement);
        }
コード例 #3
0
        public IActionResult Create([FromBody] AdvertisementCreationModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    int.TryParse(model.CategoryId, out int categoryId);

                    // get category
                    var category = _repository.GetCategory(categoryId);

                    var advertisement = new Advertisement()
                    {
                        Title       = model.Title,
                        Description = model.Description,
                        Category    = category
                    };

                    _repository.AddEntity(advertisement);

                    if (_repository.SaveAll())
                    {
                        // Saved and return the body
                        return(Created($"/api/advertisements/{advertisement.AdvertisementId}", _mapper.Map <Advertisement, AdvertisementViewModel>(advertisement)));
                    }
                    else
                    {
                        return(BadRequest("Could not save for some reason"));
                    }
                }
                else
                {
                    return(BadRequest(ModelState));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Failed to save a new advertisement: {ex}");
            }
            return(BadRequest("Creating new advertisement failed!"));
        }