public async Task <IActionResult> Create(AgentAvailabilityViewModel agentAvailability) { //Generate number Random rd = new Random(); Int32 batchCode = rd.Next(9999); agentAvailability.BatchNo = "A-" + batchCode.ToString(); if (ModelState.IsValid) { AgentAvailability availability = new AgentAvailability(); { availability.Id = agentAvailability.Id; availability.AgentId = agentAvailability.AgentId; availability.BatchNo = agentAvailability.BatchNo; availability.StartDate = agentAvailability.StartDate; availability.EndDate = agentAvailability.EndDate; availability.Days = agentAvailability.Days; availability.Shift1 = agentAvailability.Shift1; availability.Shift2 = agentAvailability.Shift2; availability.Shift3 = agentAvailability.Shift3; availability.Shift4 = agentAvailability.Shift4; } _context.Add(availability); await _context.SaveChangesAsync(); AgentAvailabilityDetailsViewModel details = new AgentAvailabilityDetailsViewModel(); int id = availability.Id; details.AgentAvailabillityId = availability.Id; Util util = new Util(); foreach (DateTime day in util.EachDay(agentAvailability.StartDate, agentAvailability.EndDate)) { //(int)day.DayOfWeek var a = (int)day.DayOfWeek; foreach (char d in agentAvailability.Days) { var val = (int)Char.GetNumericValue(d); if (a == val) { details.Date = day; _context.Add(details.GetModel()); await _context.SaveChangesAsync(); } } } return(RedirectToAction(nameof(Index))); } ViewData["AgentId"] = new SelectList(_context.Agent, "Id", "Id", agentAvailability.AgentId); agentAvailability.loadLists(_context); return(View(agentAvailability)); }
public async Task <ApiResult <AgentAvailabilityViewModel> > PostAgentAvailability([FromBody] AgentAvailabilityViewModel agentAvailability) { ApiResult <AgentAvailabilityViewModel> result = new ApiResult <AgentAvailabilityViewModel>(); //Generate number Random rd = new Random(); Int32 batchCode = rd.Next(9999); agentAvailability.BatchNo = "A-" + batchCode.ToString(); if (ModelState.IsValid) { result.Successfull = 0; try { AgentAvailability availability = new AgentAvailability(); { availability.Id = agentAvailability.Id; availability.AgentId = agentAvailability.AgentId; availability.BatchNo = agentAvailability.BatchNo; availability.StartDate = agentAvailability.StartDate; availability.EndDate = agentAvailability.EndDate; availability.Days = agentAvailability.Days; availability.Shift1 = agentAvailability.Shift1; availability.Shift2 = agentAvailability.Shift2; availability.Shift3 = agentAvailability.Shift3; availability.Shift4 = agentAvailability.Shift4; } _context.Add(availability); await _context.SaveChangesAsync(); AgentAvailabilityDetailsViewModel details = new AgentAvailabilityDetailsViewModel(); int id = availability.Id; details.AgentAvailabillityId = availability.Id; Util util = new Util(); foreach (DateTime day in util.EachDay(agentAvailability.StartDate, agentAvailability.EndDate)) { //(int)day.DayOfWeek var a = (int)day.DayOfWeek; foreach (char d in agentAvailability.Days) { var val = (int)Char.GetNumericValue(d); if (a == val) { details.Date = day; _context.Add(details.GetModel()); await _context.SaveChangesAsync(); } } } result.Successfull = 1; result.Model = agentAvailability; } catch (Exception ex) { result.Error = "Failed to Create Availability"; result.InternalError = ex.Message; result.Successfull = 0; } } //return CreatedAtAction("GetAgentAvailability", new { id = agentAvailability.Id }, agentAvailability); return(result); }