Пример #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 (AgendaMngEntities context = CreateContext())
                {
                    Appointment dbItem = context.Appointment.FirstOrDefault(o => o.AppointmentID == 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 (AppointmentAttachedFile dbFile in dbItem.AppointmentAttachedFile.ToArray())
                    {
                        if (!string.IsNullOrEmpty(dbFile.FileUD))
                        {
                            fwFactory.RemoveImageFile(dbFile.FileUD);
                        }
                        dbItem.AppointmentAttachedFile.Remove(dbFile);
                        context.AppointmentAttachedFile.Remove(dbFile);
                    }
                    context.Appointment.Remove(dbItem);
                    context.SaveChanges();

                    return(true);
                }
            }
            catch (Exception ex)
            {
                notification = new Library.DTO.Notification()
                {
                    Message = ex.Message, Type = Library.DTO.NotificationType.Error
                };
                return(false);
            }
        }
Пример #2
0
        public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification)
        {
            DTO.AppointmentDTO dtoAppointment = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.AppointmentDTO>();
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            try
            {
                using (AgendaMngEntities context = CreateContext())
                {
                    Appointment dbItem = null;
                    if (id <= 0)
                    {
                        dbItem = new Appointment();
                        if (dtoAppointment.MeetingLocationID == 5) // trip to VN
                        {
                            dbItem.UserID = dtoAppointment.UserID;
                        }
                        else
                        {
                            dbItem.UserID = userId;
                        }
                        context.Appointment.Add(dbItem);
                    }
                    else
                    {
                        dbItem = context.Appointment.FirstOrDefault(o => o.AppointmentID == 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.AgendaMngUser.Local.Where(o => o.Appointment == null).ToList().ForEach(o => context.AgendaMngUser.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.AppointmentUD = dbItem.AppointmentID.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);
            }
        }