public async Task <ActionResult <IEnumerable <CustomerActionDto> > > GetCustomersActions(long customerId) { try { var customerItem = await _context.CustomerItems.FindAsync(customerId); if (customerItem == null) { return(NotFound()); } var actions = _context.CustomerActionItems.Where(x => x.CustomerKey == customerItem.CustomerKey); List <CustomerActionDto> customerActionDtos = new List <CustomerActionDto>(); foreach (var item in actions) { CustomerActionDto dto = new CustomerActionDto { Id = item.Id, CustomerKey = item.CustomerKey, Action = item.Action, ActionDateTime = item.ActionDateTime.ToString("yyyy-MM-dd HH:mm:ss"), IsComplete = item.IsComplete }; customerActionDtos.Add(dto); } return(customerActionDtos); } catch (Exception ex) { return(StatusCode(500, ex.Message)); } }
public async Task <IActionResult> PutCustomerAction(long customerId, long id, CustomerActionDto customerActionDto) { try { var customerItem = await _context.CustomerItems.FindAsync(customerId); if (customerItem == null) { return(BadRequest()); } var customerActionItem = await _context.CustomerActionItems.FindAsync(id); if (customerActionItem == null) { return(BadRequest()); } customerActionItem.Action = customerActionDto.Action; customerActionItem.IsComplete = customerActionDto.IsComplete; customerActionItem.ActionDateTime = DateTime.Now; _context.Entry(customerActionItem).State = EntityState.Modified; await _context.SaveChangesAsync(); return(NoContent()); } catch (Exception ex) { return(StatusCode(500, ex.Message)); } }
public async Task <ActionResult <CustomerActionDto> > CreateCustomerAction(long customerId, CustomerActionDto customerActionDto) { try { var customerItem = await _context.CustomerItems.FindAsync(customerId); if (customerItem == null) { return(BadRequest()); } CustomerAction customerAction = new CustomerAction() { CustomerKey = customerItem.CustomerKey, Action = customerActionDto.Action, ActionDateTime = DateTime.Now, IsComplete = false }; _context.CustomerActionItems.Add(customerAction); await _context.SaveChangesAsync(); return(CreatedAtAction(nameof(CreateCustomerAction), new { id = customerAction.CustomerKey }, customerAction)); } catch (Exception ex) { return(StatusCode(500, ex.Message)); } }