public async Task <IActionResult> Create([Bind("ApartmentId,Address,Photo,RoomsNumber")] Apartment apartment, IFormFile files)
        {
            if (ModelState.IsValid)
            {
                // Getting the long lat
                var location = AddLongLatAsync(apartment.Address);
                apartment.Latitude  = location.Result.Latitude;
                apartment.Longitude = location.Result.Longitude;
                SavePhoto(apartment, files);
                _context.Add(apartment);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(apartment));
        }
Beispiel #2
0
        public async Task <IActionResult> Create([Bind("CurrentApartment")] int CurrentApartment, [Bind("RequestedBy")] string RequestedBy, [Bind("MalfunctionId,Status,Title,Content,Resources,CurrentApartmentId,RequestedById")] Malfunction malfunction, IFormFile mFiles)
        {
            if (ModelState.IsValid)
            {
                // Creating the query of the apartment
                var queryApt = from apt in _context.Apartment
                               where apt.ApartmentId == CurrentApartment
                               select apt;

                // Creating the query of the user
                var queryUsr = from usr in _context.User
                               where usr.Id == RequestedBy
                               select usr;

                // If the id of the apartment/user does not exist in DB
                if (!queryApt.Any() || !queryApt.Any())
                {
                    return(View(malfunction));
                }

                // Adding the apartment to the malfunction to save
                var curApartment = queryApt.First();
                malfunction.CurrentApartment = curApartment;

                // Adding the user to the malfunction to save
                var curUser = queryUsr.First();
                malfunction.RequestedBy = curUser;

                // Adding the creation date and modification date
                malfunction.CreationDate = DateTime.Now;
                malfunction.ModifiedDate = DateTime.Now;

                // Uploading photo describing the malfunction
                SavePhoto(malfunction, mFiles);

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

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