예제 #1
0
        /// <summary>
        /// Updates the specified figure.
        /// </summary>
        /// <param name="figure">The figure.</param>
        public void Update(Figure figure)
        {
            //var oldFigure = this.Context.Figures.FirstOrDefault(x => x.Id == figure.Id);
            //if (oldFigure == null) return;
            //oldFigure.Name = figure.Name;
            //oldFigure.Description = figure.Description;
            //// oldFigure.LastUpdatedUser = AppContext.LoggedInUser.Id;
            //oldFigure.LastUpdatedDate = DateTime.Now;
            //oldFigure.Version++;
            //this.Context.SaveChanges();

            try
            {

                var originalFigure = this.Context.Figures.FirstOrDefault(x => x.Id == figure.Id);
                if (originalFigure == null) throw new Exception("Không tồn tại dữ liệu trong CSDL.");

                var figureList = this.Context.FigureDetails.Where(x => x.FigureId == figure.Id).ToList();

                originalFigure.Name = figure.Name;
                originalFigure.ClinicId = figure.ClinicId;
                originalFigure.Description = figure.Description;
                originalFigure.Version++;

                foreach (var orginItem in figureList)
                {
                    var item = figure.FigureDetail.FirstOrDefault(x => x.Id == orginItem.Id);
                    if (item == null)
                    {
                        this.Context.FigureDetails.Remove(orginItem);
                    }
                    else
                    {
                        orginItem.MedicineId = item.MedicineId;
                        orginItem.FigureId = item.FigureId;
                        orginItem.Volumn = item.Volumn;
                        orginItem.Version++;
                    }

                }

                foreach (var orginItem in figure.FigureDetail)
                {
                    var item = originalFigure.FigureDetail.FirstOrDefault(x => x.Id == orginItem.Id);
                    if (item != null) continue;
                    var newItem = new FigureDetail
                    {
                        Volumn = orginItem.Volumn,
                        FigureId = orginItem.FigureId,
                        MedicineId = orginItem.MedicineId,
                        Version = 0
                    };
                    originalFigure.FigureDetail.Add(newItem);
                }

                originalFigure.LastUpdatedDate = DateTime.Now;

                this.Context.SaveChanges();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
 /// <summary>
 /// Inserts the specified user.
 /// </summary>
 /// <param name="user">The user.</param>
 public void Insert(FigureDetail user)
 {
     throw new NotImplementedException();
 }