Beispiel #1
0
        public bool DeleteAppointment(int userId, int id, out Library.DTO.Notification notification)
        {
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            try
            {
                using (PurchasingCalendarMngEntities context = CreateContext())
                {
                    PurchasingCalendarAppointment dbItem = context.PurchasingCalendarAppointment.FirstOrDefault(o => o.PurchasingCalendarAppointmentID == id);
                    if (dbItem == null)
                    {
                        throw new Exception("Event not found!");
                    }
                    if (dbItem.MeetingLocationID.Value != 5 && dbItem.UserID.Value != userId)
                    {
                        throw new Exception("You can not delete this event, please contact the event owner!");
                    }

                    // delete the event
                    foreach (PurchasingCalendarAppointmentAttachedFile dbFile in dbItem.PurchasingCalendarAppointmentAttachedFile.ToArray())
                    {
                        if (!string.IsNullOrEmpty(dbFile.FileUD))
                        {
                            fwFactory.RemoveImageFile(dbFile.FileUD);
                        }
                        dbItem.PurchasingCalendarAppointmentAttachedFile.Remove(dbFile);
                        context.PurchasingCalendarAppointmentAttachedFile.Remove(dbFile);
                    }
                    context.PurchasingCalendarAppointment.Remove(dbItem);
                    context.SaveChanges();

                    return(true);
                }
            }
            catch (Exception ex)
            {
                notification = new Library.DTO.Notification()
                {
                    Message = ex.Message, Type = Library.DTO.NotificationType.Error
                };
                return(false);
            }
        }
Beispiel #2
0
        public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification)
        {
            PurchasingCalendarAppointmentDTO dtoAppointment = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <PurchasingCalendarAppointmentDTO>();

            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            try
            {
                using (PurchasingCalendarMngEntities context = CreateContext())
                {
                    PurchasingCalendarAppointment dbItem = null;
                    if (id <= 0)
                    {
                        dbItem = new PurchasingCalendarAppointment();
                        if (dtoAppointment.MeetingLocationID == 5) // trip to VN
                        {
                            dbItem.UserID = dtoAppointment.UserID;
                        }
                        else
                        {
                            dbItem.UserID = userId;
                        }
                        context.PurchasingCalendarAppointment.Add(dbItem);
                    }
                    else
                    {
                        dbItem = context.PurchasingCalendarAppointment.FirstOrDefault(o => o.PurchasingCalendarAppointmentID == id);
                        if (dbItem == null)
                        {
                            throw new Exception("Event not found!");
                        }
                        if (dtoAppointment.MeetingLocationID == 5) // trip to VN
                        {
                            dbItem.UserID = dtoAppointment.UserID;
                        }
                        //if (dbItem.UserID.Value != userId)
                        //{
                        //    throw new Exception("You can not delete this event, please contact the event owner!");
                        //}
                    }
                    if (dtoAppointment.MeetingLocationID == 5) // trip to VN
                    {
                        dtoAppointment.StartTime = "00:00";
                        dtoAppointment.EndTime   = "23:59";
                    }

                    converter.DTO2DB(dtoAppointment, ref dbItem, FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", userId);
                    context.PurchasingCalendarUser.Local.Where(o => o.PurchasingCalendarAppointment == null).ToList().ForEach(o => context.PurchasingCalendarUser.Remove(o));
                    // updated by, updated date, meeting minute.
                    dbItem.MeetingMinute = dtoAppointment.MeetingMinute;
                    dbItem.UpdatedBy     = userId;
                    dbItem.UpdatedDate   = DateTime.Now;

                    context.SaveChanges();

                    // generate agenda code
                    if (id <= 0)
                    {
                        using (DbContextTransaction scope = context.Database.BeginTransaction())
                        {
                            context.Database.ExecuteSqlCommand("SELECT * FROM Appointment WITH (TABLOCKX, HOLDLOCK)");

                            try
                            {
                                dbItem.PurchasingCalendarAppointmentUD = dbItem.PurchasingCalendarAppointmentID.ToString("D10");
                                context.SaveChanges();
                            }
                            catch (Exception ex)
                            {
                                throw ex;
                            }
                            finally
                            {
                                scope.Commit();
                            }
                        }
                    }
                    else
                    {
                        context.SaveChanges();
                    }

                    return(true);
                }
            }
            catch (Exception ex)
            {
                notification = new Library.DTO.Notification()
                {
                    Message = ex.Message, Type = Library.DTO.NotificationType.Error
                };
                return(false);
            }
        }