public IActionResult CreateSchedule([FromBody] ScheduleForCreationDto schedule) { try { if (schedule == null) { _logger.LogError("Schedule object sent from client is null."); return(BadRequest("Schedule object is null")); } if (!ModelState.IsValid) { _logger.LogError("Invalid Schedule object sent from client."); return(BadRequest("Invalid model object")); } bool succes = _scheduleLogic.Create(schedule); return(Ok("Schedule is created")); } catch (Exception ex) { return(StatusCode(500, "Internal server error")); } }
public async Task <ActionResult <ScheduleDto> > NewSchedule(ScheduleForCreationDto schedule) { try { var userId = int.Parse(User.FindFirst("id").Value); if (await _schedulesRepository.ScheduleExistsAsync(userId, schedule.Name)) { return(Conflict()); } var newSchedule = _mapper.Map <Schedule>(schedule); newSchedule.UserId = userId; _dbRepository.Add(newSchedule); if (await _dbRepository.SaveChangesAsync()) { return(CreatedAtAction(nameof(GetSchedule), new { scheduleId = newSchedule.Id }, _mapper.Map <ScheduleDto>(newSchedule))); } } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError, "Database Failure")); } return(BadRequest()); }
public bool Create(ScheduleForCreationDto scheduleForCreation) { try { Schedule DataEntity = _mapper.Map <Schedule>(scheduleForCreation); _repository.Create(DataEntity); _repository.Save(); return(true); } catch (Exception ex) { _logger.LogError($"Something went wrong inside CreateSchedule action: {ex.Message}"); throw new Exception(); } }
public async Task <IActionResult> UpdateSchedule(int userId, int Id, ScheduleForCreationDto scheduleForUpdateDto) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var scheduleFromRepo = await _repo.GetScheduledTask(Id); _mapper.Map(scheduleForUpdateDto, scheduleFromRepo); if (await _repo.SaveAll()) { return(CreatedAtRoute("GetScheduledTask", new { Id = scheduleFromRepo.Id, userId = userId }, scheduleFromRepo)); } throw new Exception($"Updating schedule item {Id} failed on save"); }
public IActionResult CreateSchedule(int systemId, [FromBody] ScheduleForCreationDto scheduleForCreation) { var system = _unitOfWork.IrigationSystemRepository.ExistIrigationSystem(systemId); if (!system) { return(BadRequest("Irrigation system not found")); } var schedueleMapped = _mapper.Map <Schedule>(scheduleForCreation); _unitOfWork.ScheduleRepository.Create(schedueleMapped); if (!_unitOfWork.Save()) { return(StatusCode(500, "A problem happened with handling your request. Try again!")); } return(CreatedAtRoute("Schedule", schedueleMapped)); }
public async Task <IActionResult> AddSchedule(int userId, ScheduleForCreationDto scheduleForCreation) { var creator = await _userRepo.GetUser(userId); if (creator.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var schedule = _mapper.Map <Schedule>(scheduleForCreation); schedule.User = creator; _repo.Add(schedule); if (await _repo.SaveAll()) { var jobToReturn = _mapper.Map <ScheduleForReturnDto>(schedule); return(CreatedAtRoute("GetScheduledTask", new { Id = schedule.Id, userId = userId }, jobToReturn)); } throw new Exception("Creation of Schedule item failed on save"); }
public async Task <HttpResponseMessage> CreateScheduleAsync(ScheduleForCreationDto schedule) { return(await _httpService.Post($"api/schedules", schedule)); }