Exemple #1
0
        public override bool DeleteData(int id, out Library.DTO.Notification notification)
        {
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            try
            {
                using (AppointmentMngEntities context = CreateContext())
                {
                    Appointment dbItem = context.Appointment.FirstOrDefault(o => o.AppointmentID == id);
                    if (dbItem == null)
                    {
                        throw new Exception("Appointment not found!");
                    }
                    context.Appointment.Remove(dbItem);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                notification.Type    = Library.DTO.NotificationType.Error;
                notification.Message = ex.Message;
                return(false);
            }

            return(true);
        }
Exemple #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 (AppointmentMngEntities context = CreateContext())
                {
                    Appointment dbItem = null;
                    if (id == 0)
                    {
                        dbItem = new Appointment();
                        context.Appointment.Add(dbItem);
                    }
                    else
                    {
                        dbItem = context.Appointment.FirstOrDefault(o => o.AppointmentID == id);
                    }

                    if (dbItem == null)
                    {
                        throw new Exception("Appointment not found!");
                    }

                    using (DbContextTransaction scope = context.Database.BeginTransaction())
                    {
                        context.Database.ExecuteSqlCommand("SELECT * FROM Appointment WITH (TABLOCKX, HOLDLOCK)");

                        try
                        {
                            converter.DTO2DB(dtoAppointment, ref dbItem);
                            dbItem.UserID = userId;
                            context.SaveChanges();
                            if (id == 0)
                            {
                                dbItem.AppointmentUD = Library.Common.Helper.formatIndex(dbItem.AppointmentID.ToString(), 10, "0");
                                context.SaveChanges();
                            }
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                        finally
                        {
                            scope.Commit();
                        }
                    }

                    dtoItem = GetData(dbItem.AppointmentID, out notification).Data;
                    return(true);
                }
            }
            catch (Exception ex)
            {
                notification.Type    = Library.DTO.NotificationType.Error;
                notification.Message = ex.Message;
                return(false);
            }
        }