コード例 #1
0
        /// <summary>
        /// Adds the day type template.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="dayTypeTemplates">The day type templates.</param>
        /// <returns></returns>
        public BaseOperationResponse AddDayTypeTemplate(string name, IList<DayTypeTemplateListDto> dayTypeTemplates)
        {
            var response = new BaseOperationResponse();

            try
            {
                var unitOfWork = SWKM.Setup.IoC.Resolve<UnitOfWork>();
                var dayTypeTemplate = unitOfWork.DayTypeTemplates.FindBy(u => u.NAME == name && u.DELETEDDATE == null).SingleOrDefault();

                if (dayTypeTemplate != null)
                {
                    response.Successful = false;
                    response.ErrorMessage = "Szablon o takiej nazwie już istnieje.";
                    return response;
                }

                var newDayTypeTemplate = new DAYTYPETEMPLATE()
                {
                    NAME = name
                };

                unitOfWork.DayTypeTemplates.Add(newDayTypeTemplate);
                unitOfWork.Save();

                newDayTypeTemplate = unitOfWork.DayTypeTemplates.FindBy(u => u.NAME == newDayTypeTemplate.NAME && u.DELETEDDATE == null).SingleOrDefault();
                //var dayTypes = new List<DAYTYPETEMPLATEDAYTYPE>();

                foreach (var dayTypeFromList in dayTypeTemplates)
                {
                    var dayType = unitOfWork.DayTypes.FindBy(d => d.ID == dayTypeFromList.ID).SingleOrDefault();

                    if (dayType != null)
                    {

                        var dayTypeTempDayType = new DAYTYPETEMPLATEDAYTYPE()
                        {
                            DAYTYPETEMPLATE = newDayTypeTemplate,
                            DAYTYPE = dayType
                        };

                        unitOfWork.DayTypeTemplateDayTypes.Add(dayTypeTempDayType);
                    }
                    else
                    {
                        response.Successful = false;
                        response.ErrorMessage = "Wybrany typ dnia nie istnieje.";
                        return response;
                    }
                }

                unitOfWork.Save();
                response.Successful = true;
            }
            catch (Exception e)
            {
                SWKM.Setup.Logger.Instance.Log.Error("Error occurred!", e);
                response.Successful = false;
                response.ErrorMessage = "Wystapił nie oczekiwany błąd przy dodawaniu szablonu typu dnia.";
            }

            return response;
        }
コード例 #2
0
        /// <summary>
        /// Edits the day type template.
        /// </summary>
        /// <param name="dayTypeTemplateId">The day type template id.</param>
        /// <param name="name">The name.</param>
        /// <param name="dayTypes">The day type templates.</param>
        /// <returns></returns>
        public BaseOperationResponse EditDayTypeTemplate(int id, string name, IList<DayTypeTemplateListDto> dayTypes)
        {
            var response = new BaseOperationResponse();

            try
            {
                var unitOfWork = SWKM.Setup.IoC.Resolve<UnitOfWork>();
                var dayTypeTemplate = unitOfWork.DayTypeTemplates.FindBy(u => u.NAME == name && u.DELETEDDATE == null && u.ID != id).SingleOrDefault();

                if (dayTypeTemplate != null)
                {
                    response.Successful = false;
                    response.ErrorMessage = "Szablon o takiej nazwie już istnieje w systemie.";
                    return response;
                }

                dayTypeTemplate = unitOfWork.DayTypeTemplates.FindBy(u => u.ID == id).SingleOrDefault();
                if (dayTypeTemplate == null)
                {
                    response.Successful = false;
                    response.ErrorMessage = "Nie ma szablonu o takim identyfikatorze.";
                    return response;
                }

                dayTypeTemplate.NAME = name;

                var toDelete = unitOfWork.DayTypeTemplateDayTypes.FindBy(dt => dt.DAYTYPETEMPLATE.ID == dayTypeTemplate.ID).ToList();
                toDelete = toDelete.Where(t => !dayTypes.Any(d => d.ID == t.DAYTYPEID)).ToList();
                foreach (var delete in toDelete)
                {
                    unitOfWork.DayTypeTemplateDayTypes.Delete(delete);
                }

                var entityDayTypes = unitOfWork.DayTypeTemplateDayTypes.FindBy(dt => dt.DAYTYPETEMPLATE.ID == dayTypeTemplate.ID).ToList();

                foreach (var dayType in dayTypes)
                {
                    var findedDayType = entityDayTypes.Where(dt => dt.DAYTYPEID == dayType.ID).SingleOrDefault();

                    if (findedDayType == null)
                    {

                        var dayTypeTempDayType = new DAYTYPETEMPLATEDAYTYPE()
                        {
                            DAYTYPETEMPLATE = dayTypeTemplate,
                            DAYTYPEID = dayType.ID
                        };

                        unitOfWork.DayTypeTemplateDayTypes.Add(dayTypeTempDayType);
                    }
                }

                unitOfWork.Save();
                response.Successful = true;
            }
            catch (Exception e)
            {
                SWKM.Setup.Logger.Instance.Log.Error("Error occurred!", e);
                response.Successful = false;
                response.ErrorMessage = "Wystapił nie oczekiwany błąd.";
            }

            return response;
        }