Exemple #1
0
        public async Task <IActionResult> Create(ResidenceEditModel model)
        {
            var user = await _userManager.GetUserAsync(HttpContext.User);

            if (ModelState.IsValid && user != null)
            {
                var residence = new Residence
                {
                    TypeId  = model.TypeId,
                    Address = model.Address,
                    Area    = model.Area,
                    Cost    = model.Cost,
                    OwnerId = user.Id
                };

                _context.Residences.Add(residence);

                foreach (KeyValuePair <Guid, String> kvp in model.Features)
                {
                    var feature = new Feature
                    {
                        ResidenceId   = residence.Id,
                        FeatureTypeId = kvp.Key,
                        Value         = kvp.Value
                    };

                    _context.Features.Add(feature);
                }
                await _context.SaveChangesAsync();

                await _updateRequestsService.UpdateRequest(residence.Id, null);

                return(RedirectToAction(nameof(Index)));
            }

            ViewBag.TypeId = new SelectList(_context.ResidenceTypes, "Id", "Name");
            return(View(model));
        }
        public async Task <IActionResult> Edit(Guid?id, RequestEditModel model)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var request = await _context.SearchRequests
                          .Include(r => r.Results)
                          .SingleOrDefaultAsync(m => m.Id == id);

            if (request == null || !_userPermissions.IsOwnerOfRequest(request))
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                request.Address = model.Address;
                request.Cost    = model.Cost;
                request.Area    = model.Area;

                foreach (KeyValuePair <Guid, String> kvp in model.Features)
                {
                    var feature = await _context.FeatureRequests
                                  .SingleOrDefaultAsync(m => m.SearchRequestId == id && m.FeatureTypeId == kvp.Key);

                    feature.Value = kvp.Value;
                }

                await _context.SaveChangesAsync();

                await _updateRequestsService.UpdateRequest(null, request.Id);

                return(RedirectToAction(nameof(Index)));
            }

            return(View(model));
        }