Ejemplo n.º 1
0
        public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification)
        {
            DTO.LeaveRequest dtoLeaveRequest = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.LeaveRequest>();
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            try
            {
                using (LeaveRequestMngEntities context = CreateContext())
                {
                    LeaveRequest dbItem = null;
                    if (id == 0)
                    {
                        dbItem = new LeaveRequest();
                        context.LeaveRequest.Add(dbItem);
                    }
                    else
                    {
                        dbItem = context.LeaveRequest.FirstOrDefault(o => o.LeaveRequestID == id);
                    }

                    if (dbItem == null)
                    {
                        notification.Message = "LeaveRequest not found!";
                        return(false);
                    }
                    else
                    {
                        // check concurrency
                        if (dbItem.ConcurrencyFlag != null && !dbItem.ConcurrencyFlag.SequenceEqual(Convert.FromBase64String(dtoLeaveRequest.ConcurrencyFlag_String)))
                        {
                            throw new Exception(Library.Helper.TEXT_CONCURRENCY_CONFLICT);
                        }

                        converter.DTO2BD(dtoLeaveRequest, ref dbItem);

                        dbItem.UpdatedDate = DateTime.Now;
                        dbItem.UpdatedBy   = userId;

                        context.SaveChanges();
                        dtoItem = GetData(dbItem.LeaveRequestID, out notification).Data;

                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                notification.Type    = Library.DTO.NotificationType.Error;
                notification.Message = ex.Message;
                return(false);
            }
        }
Ejemplo n.º 2
0
        public override bool DeleteData(int id, out Library.DTO.Notification notification)
        {
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            try
            {
                using (LeaveRequestMngEntities context = CreateContext())
                {
                    LeaveRequest dbItem = context.LeaveRequest.FirstOrDefault(o => o.LeaveRequestID == id);
                    if (dbItem == null)
                    {
                        notification.Message = "LeaveRequest not found!";
                        return(false);
                    }
                    else
                    {
                        context.LeaveRequest.Remove(dbItem);
                        context.SaveChanges();

                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                notification.Type    = Library.DTO.NotificationType.Error;
                notification.Message = ex.Message;
                notification.DetailMessage.Add(ex.Message);
                if (ex.GetBaseException() != null)
                {
                    notification.DetailMessage.Add(ex.GetBaseException().Message);
                }
                return(false);
            }
        }