public async Task <ActionResult> AddWorkPlan([FromBody] WorkPlanDto workPlanDto) { int id = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value); var logineduser = await _auth.VerifyUser(id); if (DateTime.Now > workPlanDto.StartDate) { return(BadRequest("Baslama zamani indiki zamandan kecmisde ola bilmez")); } if (DateTime.Now > workPlanDto.EndDate) { return(BadRequest("Bitme vaxt zamani indiki zamandan kecmisde ola bilmez")); } WorkPlan newPlan = new WorkPlan() { Name = workPlanDto.Name, StartDate = workPlanDto.StartDate, EndDate = workPlanDto.EndDate, Action = await _context.Actions.FirstOrDefaultAsync(s => s.Id == 4), Created = logineduser, Company = logineduser.Company, Status = true, Parcel = _context.Parcels.FirstOrDefault(s => s.Id == workPlanDto.ParcelId) }; try { await _context.WorkPlans.AddAsync(newPlan); await _context.SaveChangesAsync(); } catch (Exception) { return(BadRequest("Emeliyyatda xeta bas verdi")); } foreach (var item in workPlanDto.WorkPlanTasks) { WorkPlanTask newtask = new WorkPlanTask() { Name = item.Name, StartDate = item.StartDate, EndDate = item.EndDate, WorkPlan = newPlan, Status = true, Respondent = _context.Workers.FirstOrDefault(s => s.Id == item.RespondentId) }; await _context.WorkPlanTasks.AddAsync(newtask); await _context.SaveChangesAsync(); if (item.WorkPlanTaskFertilizers != null) { foreach (var itemFeltilizer in item.WorkPlanTaskFertilizers) { WorkPlanTaskFertilizer _newTaskFertilizer = new WorkPlanTaskFertilizer() { Product = _context.Products.FirstOrDefault(s => s.Id == itemFeltilizer.ProductId), Quantity = itemFeltilizer.Quantity, WorkPlanTask = newtask }; await _context.WorkPlanTaskFertilizers.AddAsync(_newTaskFertilizer); await _context.SaveChangesAsync(); } } } return(StatusCode(201)); }
public JsonResult UpdateWorkPlan(WorkPlanDto workPlan) { return(Json(_mapper.Map <List <WorkPlanDto> >(_adminService.UpdateWorkPlan(_mapper.Map <WorkPlan>(workPlan))))); }