public HttpResponseMessage updateDeliverable([FromBody] DeliverableModel request)
        {
            HttpResponseMessage responseMessage;
            var message   = "deliverable successfully updated";
            var isSuccess = false;

            using (EverestPortalContext context = new EverestPortalContext())
            {
                var updateDeliverable = context.deliverables.FirstOrDefault(d => d.DeliverableId == request.DeliverableId);
                updateDeliverable.ReportWriterId = request.ReportWriterId;
                updateDeliverable.StartDate      = new DateTime(request.StartDate.Year, request.StartDate.Month, 1);                                       //change into first day of the month
                updateDeliverable.EndDate        = new DateTime(request.EndDate.Year, request.EndDate.Month, DateTime.DaysInMonth(request.EndDate.Year,
                                                                                                                                  request.EndDate.Month)); //change into last day of the month
                updateDeliverable.FrequencyTypeId = request.FrequencyTypeId;
                updateDeliverable.FrequencyId     = request.FrequencyId;
                updateDeliverable.RestrictionId   = request.RestrictionId;
                updateDeliverable.PeriodId        = request.PeriodId;
                updateDeliverable.LastModified    = DateTime.Now;
                updateDeliverable.ModifiedBy      = 1;

                context.SaveChanges();
                isSuccess = true;
            }

            var response = new
            {
                msg = message,
                isSuccess
            };

            responseMessage = Request.CreateResponse(HttpStatusCode.OK, response);
            return(responseMessage);
        }
        private dynamic validateRequest(DeliverableModel request)
        {
            var isSuccess = true;
            var msg       = "";

            if (string.IsNullOrEmpty(request.ReportNo))
            {
                msg       = "Report number cannot be empty.";
                isSuccess = false;
            }
            else
            {
                var reportNo = 0;
                if (int.TryParse(request.ReportNo, out reportNo))
                {
                    if (reportNo <= 0)
                    {
                        msg       = "Report number needs to be greater than zero.";
                        isSuccess = false;
                    }
                    else
                    {
                        using (EverestPortalContext context = new EverestPortalContext())
                        {
                            var reportNoEntry = (from d in context.deliverables
                                                 join sub in context.subscription
                                                 on d.SubscriptionId equals sub.SubscriptionId
                                                 join cl in context.Clients
                                                 on sub.ClientId equals cl.Id
                                                 join dr in context.DeliveryReports
                                                 on d.DeliverableId equals dr.DeliverableId
                                                 where dr.ReportNo == reportNo && cl.Id == request.ClientId
                                                 select d
                                                 ).ToList();
                            if (reportNoEntry != null && reportNoEntry.Count > 0)
                            {
                                msg       = "Report number already exists for the client.";
                                isSuccess = false;
                            }
                        }
                    }
                }
                else
                {
                    msg       = "Report number needs to be an integer.";
                    isSuccess = false;
                }
            }


            var result = new
            {
                message = msg,
                isSuccess
            };

            return(result);
        }
        private bool IsDeliverableExists(DeliverableModel deliObj)
        {
            var isExists = false;

            using (EverestPortalContext context = new EverestPortalContext())
            {
                if (context.deliverables.Any(x => (x.SubscriptionId == deliObj.SubscriptionId && x.DeliveryTypeId == deliObj.DeliveryTypeId && x.FrequencyTypeId == deliObj.FrequencyTypeId)))
                {
                    isExists = true;
                }
            }
            return(isExists);
        }
        public HttpResponseMessage CreateDeliverable([FromBody] DeliverableModel request)
        {
            try
            {
                HttpResponseMessage responseMessage;
                var message   = "deliverable successfully created for the selected subscription";
                var isSuccess = false;

                var validationResult = validateRequest(request);
                if (!validationResult.isSuccess)
                {
                    responseMessage = Request.CreateResponse(HttpStatusCode.OK, new { message = validationResult.message, validationResult.isSuccess });
                    return(responseMessage);
                }
                var reportNo = Convert.ToInt32(request.ReportNo);

                using (EverestPortalContext context = new EverestPortalContext())
                {
                    if (!IsDeliverableExists(request))
                    {
                        //adding the new deliverables to the selected clients
                        var deliverable = new Deliverables()
                        {
                            SubscriptionId = request.SubscriptionId,
                            ReportWriterId = request.ReportWriterId,
                            StartDate      = new DateTime(request.StartDate.Year, request.StartDate.Month, 1),                                       //change into first day of the month
                            EndDate        = new DateTime(request.EndDate.Year, request.EndDate.Month, DateTime.DaysInMonth(request.EndDate.Year,
                                                                                                                            request.EndDate.Month)), //change into last day of the month
                            FrequencyTypeId = request.FrequencyTypeId,
                            FrequencyId     = request.FrequencyId,
                            RestrictionId   = request.RestrictionId,
                            PeriodId        = request.PeriodId,
                            DeliveryTypeId  = request.DeliveryTypeId,
                            LastModified    = DateTime.Now,
                            ModifiedBy      = 1,
                        };
                        deliverable = context.deliverables.Add(deliverable);
                        context.SaveChanges();
                        isSuccess = true;
                        //add reportno.
                        //context.DeliveryReports.Add(new DeliveryReport() { DeliverableId = 218,
                        //    ReportNo = reportNo });
                        //context.SaveChanges();

                        AddReportNoDeliverable(new DeliveryReport()
                        {
                            DeliverableId = deliverable.DeliverableId, ReportNo = reportNo
                        });
                    }
                    else
                    {
                        message = "Already same deliverable exists for the selected subscription";
                    }
                }

                var response = new
                {
                    msg = message,
                    isSuccess
                };

                responseMessage = Request.CreateResponse(HttpStatusCode.OK, response);
                return(responseMessage);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }