Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. [HttpPut("{id}")]
  2. public async Task<object> EditEventOccurrence(int id, [FromBody] EventOccurrenceResource eventOccurrenceResource)
  3. {
  4. if (!ModelState.IsValid)
  5. return BadRequest(ModelState);
  6.  
  7. var eventOccurrence = await _context.EventOccurrences
  8. .Where(eo => eo.Id == id)
  9. .Include(eo => eo.DeviceOperation)
  10. .ThenInclude(devop => devop.Codes)
  11. .ThenInclude(co => co.Code)
  12. .Include(eo => eo.DeviceOperation)
  13. .ThenInclude(devop => devop.Breaker.SupplySubstation)
  14. .Include(eo => eo.Outage)
  15. .ThenInclude(o => o.Codes)
  16. .ThenInclude(co => co.Code)
  17. .Include(eo => eo.Outage)
  18. .ThenInclude(o => o.Substation)
  19. .SingleOrDefaultAsync();
  20. if (eventOccurrence == null)
  21. {
  22. return NotFound($"Event Occurrence {id} was not found");
  23. }
  24.  
  25. _mapper.Map<EventOccurrenceResource, EventOccurrence>(eventOccurrenceResource, eventOccurrence);
  26.  
  27. await _context.SaveChangesAsync();
  28.  
  29. var result = _mapper.Map<EventOccurrence, EventOccurrenceResource>(eventOccurrence, eventOccurrenceResource);
  30.  
  31. return Ok(result);
  32. }
  33.  
  34. public class EventOccurrenceResource
  35. {
  36. public int Id { get; set; }
  37.  
  38. [Required]
  39. public int EventId { get; set; }
  40. public EventResource Event { get; set; }
  41.  
  42. public DeviceOperationResource DeviceOperation { get; set; }
  43.  
  44. public OutageResource Outage { get; set; }
  45. }
  46.  
  47. public class EventOccurrence
  48. {
  49. public int Id { get; set; }
  50.  
  51. public Event Event { get; set; }
  52. public int EventId { get; set; }
  53.  
  54. public DeviceOperation DeviceOperation { get; set; }
  55. public int? DeviceOperationId { get; set; }
  56.  
  57. public Outage Outage { get; set; }
  58. public int? OutageId { get; set; }
  59. }
  60.  
  61. CreateMap<EventOccurrenceResource, EventOccurrence>()
  62. .ForMember(eo => eo.Id, opt => opt.Ignore())
  63. .ForMember(eo => eo.OutageId, opt => opt.MapFrom(eor => eor.Outage.Id));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement