private static void AllocateBonus(BonusRecipients bonusRecipients) { HttpClient client = GetHttpClient(); StringContent content = new StringContent(JsonConvert.SerializeObject(bonusRecipients), Encoding.UTF8, "application/json"); client.PostAsync("api/Remuneration", content); }
public HttpResponseMessage Post(BonusRecipients br) { IList <EmployeeBonusInfo> list = new List <EmployeeBonusInfo>(); DateTime dt = DateTime.UtcNow; foreach (var employee in br.Recipients) { EmployeeBonusInfo ebi = new EmployeeBonusInfo(); ebi.BonusAmount = br.BonusAmount; ebi.EmployeeNo = employee.EmployeeNo; ebi.BonusDate = dt; list.Add(ebi); } WriteXML(list); return(Request.CreateResponse(HttpStatusCode.OK));; }
public async Task <ActionResult> Allocate(BonusViewModel bonusAllocation) { try { //commonet by pmalik: ConfigureAwait(false) is used so that next line after async call should go to next //available thread from threadpool instead of waiting for the original thread to become free var employees = await GetEmployees().ConfigureAwait(false); List <Employee> recipients = new List <Employee>(); for (int i = 0; i < employees.Count; i++) { if (i % bonusAllocation.OneInXEmployees == 0) { var recipient = employees[i]; recipients.Add(recipient); /*comment by pmalik: * Removing recipient from employees list was reducing its employees count * and hence the money assigned to pay out for bonuses was not all being paid out */ //employees.Remove(recipient); } } var bonusRecipients = new BonusRecipients() { BonusAmount = bonusAllocation.Amount, Recipients = recipients }; AllocateBonus(bonusRecipients); return(RedirectToAction("Index", "Home")); } catch { return(View("View")); } }
public async Task <IHttpActionResult> Post(BonusRecipients bonusRecipients) { try { if (bonusRecipients == null) { return(BadRequest("Bonus recipients data is not initilised")); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var employeeBonusList = new List <EmployeeBonus>(); bonusRecipients?.Recipients?.ToList() .ForEach(x => employeeBonusList.Add(new EmployeeBonus { EmployeeNo = x.EmployeeNo, BonusDate = DateTime.Now, BonusAmount = bonusRecipients.BonusAmount })); if (await _bonusRepository.SaveAsync(employeeBonusList).ConfigureAwait(false)) { return(StatusCode(HttpStatusCode.Accepted)); } else { return(BadRequest("Failed to save bonus recipients data")); } } catch (Exception ex) { return(InternalServerError(new Exception(ex.Message))); } }
public IHttpActionResult AddBonus(BonusRecipients bonusRecipients) { if (!ModelState.IsValid) { return(BadRequest()); } var employeesWithBonus = bonusRecipients.Recipients.Select(r => new EmployeeWithBonus { EmployeeNo = r.EmployeeNo, Date = DateTime.Now, BonusAmount = bonusRecipients.BonusAmount }).ToArray(); var employees = new EmployeesWithBonus { Employees = employeesWithBonus }; var saved = SaveToXml(employees); if (!saved) { return(BadRequest()); } return(Ok()); }