public IActionResult CreateCase(CaseExpanded request) { if (!ModelState.IsValid) { return BadRequest(); } Case creating = new Case { Title = request.Title, ClientId = Client.Id, OrderId = request.OrderId }; CaseMessageData message = request.Messages.FirstOrDefault(); if (message != null) { CaseMessage messageTo = new CaseMessage { CaseId = creating.Id, Contents = message.Contents, ClientId = Client.Id }; creating.CaseMessages.Add(messageTo); if (request.Attachments.Count > 0) { CaseAttachmentDTO requestedAttachment = request.Attachments.FirstOrDefault(); if (requestedAttachment != null) { messageTo.CaseAttachments.Add(new CaseAttachment { AttachmentUrl = requestedAttachment.AttachmentUrl, CaseMessageId = message.Id }); } } } else { return BadRequest(new { message = "Byla privalo turėti žinutę." }); } _context.Cases.Add(creating); _context.SaveChanges(); return Ok(new { message = "Nauja byla sukurta." }); }
public IActionResult WriteCaseMessage(int caseId, CaseMessageData request) { Case writingTo = _context.Cases .SingleOrDefault(c => c.Id == caseId && c.Status != 1); if (writingTo == null) { return(BadRequest(new { message = "Tokia byla nerasta." })); } if (string.IsNullOrEmpty(request.Contents)) { return(BadRequest(new { message = "Paliktas tuščias žinutės laukas." })); } CaseMessage message = new CaseMessage { Contents = request.Contents, CaseId = writingTo.Id, ClientId = Client.Id }; if (request.Attachments.Count > 0) { foreach (var attachment in request.Attachments) { message.CaseAttachments.Add(new CaseAttachment() { CaseMessageId = message.Id, AttachmentUrl = attachment.AttachmentUrl }); } } writingTo.UpdatedAt = DateTime.Now; writingTo.CaseMessages.Add(message); _context.SaveChanges(); return(Ok(new { message = "Nauja žinutė pridėta." })); }