Ejemplo n.º 1
0
        public async Task <ParentFullServiceModel> UpdateParentAsync(ParentFullServiceModel parent)
        {
            ValidateParentId(parent.Id);
            ValidateIfParentIsDeleted(parent.Id);
            ValidateIfInputStringIsNotNullOrEmpty(parent.FirstName);
            ValidateIfInputStringIsNotNullOrEmpty(parent.MiddleName);
            ValidateIfInputStringIsNotNullOrEmpty(parent.LastName);

            var parentInDb = await db.Parents.FindAsync(parent.Id);

            parentInDb.FirstName   = parent.FirstName;
            parentInDb.MiddleName  = parent.MiddleName;
            parentInDb.LastName    = parent.LastName;
            parentInDb.Description = parent.Description;
            parentInDb.Image       = parent.Image;
            parentInDb.Address     = parent.Address;
            parentInDb.Email       = parent.Email;
            parentInDb.Phone       = parent.Phone;
            await db.SaveChangesAsync();

            var parentInDbUpdated = await db.Parents
                                    .Include(x => x.Students)
                                    .ThenInclude(studentParent => studentParent.Student)
                                    .FirstOrDefaultAsync(p => p.Id == parent.Id);

            return(this.mapper.Map <ParentFullServiceModel>(parentInDbUpdated));
        }
Ejemplo n.º 2
0
        public async Task <ParentFullServiceModel> CreateParentAsync(ParentFullServiceModel parent)
        {
            ValidateIfInputStringIsNotNullOrEmpty(parent.FirstName);
            ValidateIfInputStringIsNotNullOrEmpty(parent.MiddleName);
            ValidateIfInputStringIsNotNullOrEmpty(parent.LastName);
            ValidateIfInputStringIsNotNullOrEmpty(parent.Email);
            ValidateIfInputStringIsNotNullOrEmpty(parent.Phone);
            ValidateIfInputStringIsNotNullOrEmpty(parent.Address);
            ValidateIfDateIsNotNull(parent.DateBirth);

            Parent parentToAddInDb = this.mapper.Map <Parent>(parent);

            await SetDefaultImagePathIfImagePathIsNull(parentToAddInDb);
            await SetDefaultDescriptionIfDescriptionIsNull(parentToAddInDb);

            await this.db.Parents.AddAsync(parentToAddInDb);

            await db.SaveChangesAsync();

            Parent parentAddedInDb = await this.db.Parents.FirstOrDefaultAsync(x => x.FirstName == parent.FirstName && x.MiddleName == parent.MiddleName && x.LastName == parent.LastName);

            return(this.mapper.Map <ParentFullServiceModel>(parentAddedInDb));
        }
        public async Task <IActionResult> Post([FromBody] ParentFullServiceModel parent)
        {
            var parentCreated = await this.parentService.CreateParentAsync(parent);

            return(Ok(parentCreated));
        }