Exemple #1
0
        public override bool UpdateData(int userId, int id, ref object dtoItem, out Notification notification)
        {
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };

            DTO.NotificationGroupDTO dataItem = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.NotificationGroupDTO>();

            try
            {
                using (var context = CreateContext())
                {
                    NotificationGroup dbItem;

                    if (id > 0)
                    {
                        dbItem = context.NotificationGroup.FirstOrDefault(s => s.NotificationGroupID == id);

                        if (dbItem == null)
                        {
                            notification.Type    = NotificationType.Error;
                            notification.Message = "Can not find data";
                            return(false);
                        }
                    }
                    else
                    {
                        dbItem = context.NotificationGroup.FirstOrDefault(s => s.NotificationGroupUD.Equals(dataItem.NotificationGroupUD));
                        if (dbItem != null)
                        {
                            notification.Type    = NotificationType.Error;
                            notification.Message = dbItem.NotificationGroupUD + " is existed in database!";
                            return(false);
                        }

                        dbItem = new NotificationGroup();
                        context.NotificationGroup.Add(dbItem);
                    }

                    converter.DTO2DB_Update(dataItem, ref dbItem);

                    context.NotificationGroupMember.Local.Where(o => o.NotificationGroup == null).ToList().ForEach(o => context.NotificationGroupMember.Remove(o));
                    context.NotificationGroupStatus.Local.Where(o => o.NotificationGroup == null).ToList().ForEach(o => context.NotificationGroupStatus.Remove(o));
                    context.SaveChanges();

                    dtoItem = converter.DB2DTO_NotificationGroup(context.NotificationMng_NotificationGroup_View.FirstOrDefault(o => o.NotificationGroupID == dbItem.NotificationGroupID));

                    return(true);
                }
            }
            catch (Exception ex)
            {
                notification.Type    = NotificationType.Error;
                notification.Message = ex.Message;
                return(false);
            }
        }
Exemple #2
0
        public void DTO2DB_Update(DTO.NotificationGroupDTO dataItem, ref NotificationGroup dbItem)
        {
            //foreach (var item in dbItem.NotificationGroupMember)
            //{
            //    //if (!dataItem.NotificationGroupMemberData.Select(s => s.NotificationGroupMemberID).Contains(item.NotificationGroupMemberID))
            //        dbItem.NotificationGroupMember.Remove(item);
            //}

            if (dataItem.NotificationGroupMemberData != null)
            {
                foreach (var item in dbItem.NotificationGroupMember.ToArray())
                {
                    if (!dataItem.NotificationGroupMemberData.Select(s => s.NotificationGroupMemberID).Contains(item.NotificationGroupMemberID))
                    {
                        dbItem.NotificationGroupMember.Remove(item);
                    }
                }

                foreach (var dto in dataItem.NotificationGroupMemberData)
                {
                    NotificationGroupMember item;

                    if (dto.NotificationGroupMemberID < 0)
                    {
                        item = new NotificationGroupMember();

                        dbItem.NotificationGroupMember.Add(item);
                    }
                    else
                    {
                        item = dbItem.NotificationGroupMember.FirstOrDefault(s => s.NotificationGroupMemberID == dto.NotificationGroupMemberID);
                    }

                    if (item != null)
                    {
                        AutoMapper.Mapper.Map <DTO.NotificationGroupMemberDTO, NotificationGroupMember>(dto, item);
                    }
                }
            }
            if (dataItem.NotificationGroupStatuses != null)
            {
                foreach (var item in dbItem.NotificationGroupStatus.ToList())
                {
                    if (!dataItem.NotificationGroupStatuses.Select(s => s.NotificationGroupStatusID).Contains(item.NotificationGroupStatusID))
                    {
                        dbItem.NotificationGroupStatus.Remove(item);
                    }
                }

                foreach (var dto in dataItem.NotificationGroupStatuses.ToList())
                {
                    NotificationGroupStatus item;

                    if (dto.NotificationGroupStatusID < 0)
                    {
                        item = new NotificationGroupStatus();

                        dbItem.NotificationGroupStatus.Add(item);
                    }
                    else
                    {
                        item = dbItem.NotificationGroupStatus.FirstOrDefault(s => s.NotificationGroupStatusID == dto.NotificationGroupStatusID);
                    }

                    if (item != null)
                    {
                        AutoMapper.Mapper.Map <DTO.NotificationGroupStatusDTO, NotificationGroupStatus>(dto, item);
                    }
                }
            }

            AutoMapper.Mapper.Map <DTO.NotificationGroupDTO, NotificationGroup>(dataItem, dbItem);
        }