Пример #1
0
        public AppointmentModel AddOrEditAppointment(AppointmentViewModel model)
        {
            PetModel petModel = new PetModel()
            {
                PetId   = model.PetId,
                PetName = model.PetName,
                Type    = model.Type,
                OwnerId = model.OwnerId
            };

            OwnerModel ownerModel = new OwnerModel()
            {
                OwnerId = model.OwnerId,
                First   = model.First,
                Last    = model.Last,
                Phone   = model.Phone,
                Address = model.Address
            };

            AppointmentModel appointmentModel = new AppointmentModel()
            {
                AppointmentId     = model.AppointmentId,
                PetId             = model.PetId,
                AppointmentDate   = model.AppointmentDate,
                AppointmentReason = model.AppointmentReason
            };

            PetService petLogic = new PetService();
            var        pet      = petLogic.AddOrEditPet(petModel, ownerModel);

            Appointment appointment = new Appointment();

            using (var db = new PawsAndClawsEntities())
            {
                appointment = db.Appointments.Where(i => i.AppointmentId == model.AppointmentId).FirstOrDefault();

                if (appointment != null)
                {
                    appointment.AppointmentDate   = model.AppointmentDate;
                    appointment.AppointmentReason = model.AppointmentReason;
                    appointment.PetId             = model.PetId;
                }
                else
                {
                    appointmentModel.PetId = pet.PetId;
                    appointment            = db.Appointments.Add(appointmentModel.ToDTO());
                }

                db.SaveChanges();
            }

            return(appointmentModel);
        }