public async Task <IActionResult> RemoveReportContact(int contactId, [FromQuery] int ReportId) { try { if (contactId < 1 || ReportId < 1) { return(new BadRequestResult()); } //check if valid site if (await agent.Find <reporting_metrics>(ReportId) == null) { return(new NotFoundResult()); } //remove from Reporting_Contact reportmetric_contact entity = agent.Select <reportmetric_contact>().FirstOrDefault(rmc => rmc.contact_id == contactId && rmc.reporting_metrics_id == ReportId); if (entity == null) { return(new NotFoundResult()); } await agent.Delete <reportmetric_contact>(entity); //sm(agent.Messages); return(Ok()); } catch (Exception ex) { //sm(agent.Messages); return(await HandleExceptionAsync(ex)); } }
public async Task <IActionResult> AddReportContact(int reportId, int contactTypeId, [FromBody] contact entity) { try { reportmetric_contact newRepContact = null; contact thisContact = null; Int32 loggedInUserId = 0; if (!isValid(entity)) { return(new BadRequestResult()); } var loggedInMember = LoggedInUser(); loggedInUserId = loggedInMember.member_id; //check if valid Contact Type if (await agent.Find <contact_type>(contactTypeId) == null) { return(new BadRequestResult()); } if (await agent.Find <reporting_metrics>(reportId) == null) { return(new BadRequestResult()); } //save the contact thisContact = await agent.Add <contact>(entity); // last_updated parts (need to do this after add in case contact already exists, don't want to make a new one) thisContact.last_updated = DateTime.Now; thisContact.last_updated_by = loggedInUserId; await agent.Update(thisContact.contact_id, thisContact); //add ReportMetrics_Contact if not already there. if (agent.Select <reportmetric_contact>().FirstOrDefault(rt => rt.contact_id == thisContact.contact_id && rt.reporting_metrics_id == reportId && rt.contact_type_id == contactTypeId) == null) { newRepContact = new reportmetric_contact(); newRepContact.reporting_metrics_id = reportId; newRepContact.contact_type_id = contactTypeId; newRepContact.contact_id = thisContact.contact_id; // last_updated parts newRepContact.last_updated = DateTime.Now; newRepContact.last_updated_by = loggedInUserId; newRepContact = await agent.Add <reportmetric_contact>(newRepContact); }//end if //sm(agent.Messages); return(Ok(thisContact)); } catch (Exception ex) { //sm(agent.Messages); return(await HandleExceptionAsync(ex)); } }