示例#1
0
        public async Task <ResponseModel <UniversityResource> > UpdateAsync(int id, University university)
        {
            var existingUniversity = await universityRepository.FindByIdAsync(id);

            if (existingUniversity == null)
            {
                return(new ResponseModel <UniversityResource>()
                {
                    Success = false,
                    Message = "University not found!"
                });
            }

            var existingCity = await cityRepository.FindByIdAsync(university.CityId);

            if (existingCity == null)
            {
                return new ResponseModel <UniversityResource>()
                       {
                           Success = false,
                           Message = "Invalid city."
                       }
            }
            ;

            existingUniversity.Name = university.Name;
            existingUniversity.City = existingCity;
            try
            {
                universityRepository.Update(existingUniversity);

                await unitOfWork.CompleteAsync();

                var universityResource = mapper.Map <University, UniversityResource>(existingUniversity);
                return(new ResponseModel <UniversityResource>()
                {
                    Success = true,
                    Data = universityResource,
                    Message = "Succesfully updated!"
                });
            }
            catch (Exception ex)
            {
                return(new ResponseModel <UniversityResource>()
                {
                    Success = false,
                    Message = $"An error occurred when updating the university: { ex.Message}"
                });
            }
        }
        public async Task <ResponseModel <FacultyResource> > SaveAsync(Faculty faculty)
        {
            try
            {
                var existingUniversity = await universityRepository.FindByIdAsync(faculty.UniversityId);

                if (existingUniversity == null)
                {
                    return new ResponseModel <FacultyResource>()
                           {
                               Success = false,
                               Message = "Invalid university."
                           }
                }
                ;
                await facultyRepository.AddAsync(faculty);

                await unitOfWork.CompleteAsync();

                var facultyResource = mapper.Map <Faculty, FacultyResource>(faculty);
                return(new ResponseModel <FacultyResource>()
                {
                    Success = true,
                    Data = facultyResource,
                    Message = "Successfully added!"
                });
            }
            catch (Exception ex)
            {
                return(new ResponseModel <FacultyResource>()
                {
                    Success = false,
                    Message = $"An error occurred when saving the faculty: { ex.Message}"
                });
            }
        }