public async Task <IActionResult> PostSchedule([FromBody] WeekScheduleDto weekScheduleDto) { var username = User.Claims.Where(c => c.Type == ClaimTypes.Name).FirstOrDefault().Value; var result = await _repository.AddSchedule(weekScheduleDto, username); if (result == "success") { return(Ok(result)); } return(BadRequest(result)); }
public async Task <string> AddSchedule(WeekScheduleDto weekScheduleDto, string username) { var walker = await GetWalkerByUserName(username); if (walker == null) { return("walker not found"); } using (var trans = await _dbContext.Database.BeginTransactionAsync()) { try { var scheds = weekScheduleDto.Week.Select(s => { var existing = _dbContext.WalkerSchedule.FirstOrDefault(e => e.WalkerId == walker.Id && e.Date.Date == s.Date.Date); if (existing != null) { existing.HoursAvailable = s.HoursAvailable; return(existing); } else { return(new WalkerSchedule { Id = 0, WalkerId = walker.Id, Date = s.Date.Date, HoursAvailable = s.HoursAvailable }); } }).ToList(); var schedsCopy = scheds.ToList(); scheds.RemoveAll(sch => sch.Id == 0); scheds.ForEach(e => { _dbContext.Attach(e).State = EntityState.Modified; }); schedsCopy.RemoveAll(sch => sch.Id != 0); await _dbContext.WalkerSchedule.AddRangeAsync(schedsCopy); await _dbContext.SaveChangesAsync(); trans.Commit(); return("success"); } catch (Exception e) { trans.Rollback(); return(e.Message); } } }
internal async Task <HttpResponseMessage> PostWalkerSchedule(WeekScheduleDto obj) { var url = "/api/walkers/schedule"; var serializedObj = JsonConvert.SerializeObject(obj); var content = new StringContent(serializedObj, Encoding.UTF8, "application/json"); _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", TempWalkerToken); var res = await _client.PostAsync(url, content); //limpiar los headers _client.DefaultRequestHeaders.Clear(); var parsedWalkRequest = await res.Content.ReadAsStringAsync(); return(res); }
public IActionResult A([FromBody] WeekScheduleDto weekScheduleDto) { return(Ok(weekScheduleDto)); }