public async Task <IActionResult> CreateLocation([FromBody] LocationViewModel locationViewModel) { try { var user = await GetUser(); var location = new Location { Id = new Guid(), Name = locationViewModel.Name.ToTitleCase(), IsSilentAlarm = locationViewModel.IsSilentAlarm, ApplicationUserId = user.Id }; await _locationRepository.CreateLocation(_locationUsersRepository, location); return(Ok(location)); } catch (Exception ex) { Logger.LogError(ex.Message); return(BadRequest(new ErrorResponse(ex))); } }
/// <summary> /// Creates a new animal and stores it in the database. In addition /// it finds current user and stores the current user as the animal's creator. /// </summary> /// <param name="model"></param> /// <returns>animal's id</returns> public async Task <int> CreateAnimal(AnimalBm model) { var animal = mapper.Map <Animal>(model); var userId = GetCurrentUser(); animal.UserId = userId; // will be better to put in constructor of DTO or Bm animal.CreatedAt = DateTime.Now; animal.UpdatedAt = DateTime.Now; var userAnimals = await context.Animals.Where(x => x.UserId == userId) .FirstOrDefaultAsync(x => x.Name == model.Name); if (userAnimals == null) { context.Add(animal); await context.SaveChangesAsync(); var animalLocation = mapper.Map <LocationBm>(model); animalLocation.AnimalId = animal.Id; await locationRepository.CreateLocation(animalLocation); logger.LogInfo($"Animal with id {animal.Id} successfully created."); return(animal.Id); } else { throw new BadRequestException($"You already registered an animal with the same name."); } }
public ActionResult Create(LocationViewModel model) { if (!ModelState.IsValid) { return(View(model)); } var location = Mapper.Map <LocationViewModel, Location>(model); if (_repository.CreateLocation(location)) { return(RedirectToAction("Index")); } return(View(model)); }
private void AddWorkout() { transaction = new TransactionScope(); dailyMetricsRepo = new SqlDailyMetricsRepository(connectionString); locationRepo = new SqlLocationRepository(connectionString); weatherRepo = new SqlWeatherRepository(connectionString); environmentRepo = new SqlEnvironmentRepository(connectionString); sessionRepo = new SqlSessionRepository(connectionString); workoutRepo = new SqlWorkoutRepository(connectionString); transaction.Dispose(); var dailyMetric = dailyMetricsRepo.CreateDailyMetrics(Date, Weight, SleepDuration, Calories); var location = locationRepo.CreateLocation(Location); var weather = weatherRepo.CreateWeather(WeatherType); var enviroment = environmentRepo.CreateEnvironment(weather.WeatherID, location.LocationID, IsIndoor); var session = sessionRepo.CreateSession(dailyMetric.MetricID, enviroment.EnvironmentID, StartTime, EndTime, Rating); var workout = workoutRepo.CreateWorkout(session.SessionID, Duration, AvgHeartRate); }
public async Task <LocationModel> Post([FromBody] LocationModel locationIn) { var location = locationIn; if (locationIn.Id > 0) { // update the existing location location = await _locationRepository.GetLocation(locationIn.Id); if (location == null) { Response.StatusCode = 404; return(null); } location.Name = locationIn.Name; return(await _locationRepository.UpdateLocation(location)); } else { // create a new location return(await _locationRepository.CreateLocation(location)); } }
public void Create(int customerId) { //TODO: Regras de Negocios _locationRepository.CreateLocation(customerId); }
public LocationDto CreateLocationItem(LocationDto location) { return(locationRepository.CreateLocation(location)); }