예제 #1
0
        public async Task <IActionResult> UpdateAsync(
            [FromBody] LibraryBookViewModel library
            )
        {
            try
            {
                var libraryDb = await _libraryBookMananger
                                .GetByIdAsync(library.Id);

                // não localizou
                if (libraryDb == null)
                {
                    return(BadRequest(new ResponseErrorViewModel
                    {
                        Status = Constants.Error,
                        Errors = new List <object>
                        {
                            new { Message = Constants.LibraryNotFound }
                        }
                    }));
                }

                var libraryBookMap = _mapper.Map <LibraryBook>(library);

                // captura objetos que estão no banco
                var typesDb = libraryDb.LibraryBookTypes.ToArray();

                if (libraryBookMap.LibraryBookTypes.Count > 0)
                {
                    // percorrer tipos dos livros cadastrados
                    foreach (var type in typesDb)
                    {
                        // verificar se existe no objeto enviado
                        if (!libraryBookMap.LibraryBookTypes
                            .Any(x => x.Type.Description.Equals(type.Type.Description)))
                        {
                            libraryDb.LibraryBookTypes.Remove(type);
                        }
                    }

                    // percorrer os novos tipos
                    foreach (var type in libraryBookMap.LibraryBookTypes)
                    {
                        if (typesDb.FirstOrDefault(x => x.Type.Description
                                                   .ToLower().Equals(type.Type.Description.ToLower())) == null)
                        {
                            var typeDb = await _typeMananger
                                         .GetByDescriptionAsync(type.Type.Description);

                            if (typeDb != null)
                            {
                                libraryDb.LibraryBookTypes.Add(
                                    new LibraryBookType
                                {
                                    Type        = typeDb,
                                    LibraryBook = libraryDb
                                });
                            }
                        }
                    }
                }

                libraryDb.UpdatedAt   = Util.CurrentDateTime();
                libraryDb.Description = libraryBookMap.Description;
                _libraryBookMananger.Update(libraryDb);

                _uow.Commit();

                return(Ok(new ResponseViewModel
                {
                    Result = null,
                    Status = Constants.Sucess
                }));
            }
            catch (Exception ex)
            {
                // gerar log
                return(BadRequest(new
                {
                    Status = Constants.Error,
                    Errors = new List <object>
                    {
                        Util.ReturnException(ex)
                    }
                }));
            }
        }
예제 #2
0
        public async Task <IActionResult> CreateAsync(
            [FromBody] CreateContactViewModel createContactViewModel
            )
        {
            try
            {
                if (createContactViewModel.IdLibraryBook <= 0)
                {
                    return(BadRequest(new ResponseErrorViewModel
                    {
                        Status = Constants.Error,
                        Errors = new List <object>
                        {
                            new { Message = Constants.LibraryNotFound }
                        }
                    }));
                }

                // buscar do usuario autenticado
                var login = Util.GetClaim(_httpContext,
                                          Constants.UserClaimIdentifier);

                var userDbRequest = await _userMananger
                                    .GetByLogin(login);

                if (userDbRequest == null)
                {
                    return(BadRequest(new ResponseErrorViewModel
                    {
                        Status = Constants.Error,
                        Errors = new List <object>
                        {
                            new { Message = Constants.UserNotFound }
                        }
                    }));
                }

                // verificar se existe contado de usuario logado
                var contactDbRequest = await _contactMananger
                                       .GetByEmail(userDbRequest.Login);

                // criar contato caso não exista
                if (contactDbRequest == null)
                {
                    contactDbRequest = new Contact
                    {
                        Email = userDbRequest.Login,
                        Name  = userDbRequest.Person.Name,
                        Phone = userDbRequest.Person.Phones
                                .SingleOrDefault(x => x.Master == true).Number,
                        Active    = true,
                        CreatedAt = Util.CurrentDateTime()
                    };
                }
                else
                {
                    // verifica se já solicitou contato para o mesmo livro
                    var contactDb = await _contactMananger
                                    .GetByIdLiraryAndContactRequest(
                        createContactViewModel.IdLibraryBook,
                        contactDbRequest.Id);

                    if (contactDb != null)
                    {
                        return(Conflict(new ResponseErrorViewModel
                        {
                            Errors = new List <object>
                            {
                                new { Message = Constants.ContactConflict }
                            },
                            Status = Constants.Error
                        }));
                    }
                }

                contactDbRequest.UpdatedAt = Util.CurrentDateTime();

                if (contactDbRequest.Id > 0)
                {
                    _contactMananger.Update(contactDbRequest);
                }
                else
                {
                    await _contactMananger.CreateAsync(contactDbRequest);
                }

                // buscar livro da biblioteca
                var libraryBook = await _libraryBookMananger
                                  .GetByIdAsync(createContactViewModel.IdLibraryBook);

                if (libraryBook == null)
                {
                    return(BadRequest(new ResponseErrorViewModel
                    {
                        Status = Constants.Error,
                        Errors = new List <object>
                        {
                            new { Message = Constants.BooksNotFound }
                        }
                    }));
                }

                // buscar o login do usuario dono da biblioteca para criar cadastro
                var userOwner = await _userMananger
                                .GetByIdAsyncNoTracking(libraryBook.Library.Person.Id);

                // verifica se o livro requisitado não é do mesmo usuario
                if (userOwner.Login.Equals(userDbRequest.Login))
                {
                    return(BadRequest(new ResponseErrorViewModel
                    {
                        Status = Constants.Error,
                        Errors = new List <object>
                        {
                            new { Message = Constants.BookInvalid }
                        }
                    }));
                }

                if (userOwner == null)
                {
                    return(BadRequest(new ResponseErrorViewModel
                    {
                        Status = Constants.Error,
                        Errors = new List <object>
                        {
                            new { Message = Constants.UserNotFound }
                        }
                    }));
                }

                var contactDbOwner = await _contactMananger
                                     .GetByEmail(userOwner.Login);

                if (contactDbOwner == null)
                {
                    contactDbOwner = new Contact
                    {
                        Name  = userOwner.Person.Name,
                        Email = userOwner.Login,
                        Phone = userOwner.Person.Phones
                                .SingleOrDefault(x => x.Master == true).Number,
                        ContactBooksOwner = new List <ContactBook>(),
                        Active            = true,
                        CreatedAt         = Util.CurrentDateTime()
                    };
                }

                contactDbOwner.UpdatedAt = Util.CurrentDateTime();

                var contactBook = new ContactBook
                {
                    ContactOwner   = contactDbOwner,
                    ContactRequest = contactDbRequest,
                    LibraryBook    = libraryBook,
                    Approved       = libraryBook.LibraryBookTypes.Any(x => x.Type.Id == 5),
                    Denied         = false,
                    CreatedAt      = Util.CurrentDateTime()
                };

                contactDbOwner.ContactBooksOwner.Add(contactBook);

                if (contactDbOwner.Id > 0)
                {
                    _contactMananger.Update(contactDbOwner);
                }
                else
                {
                    await _contactMananger.CreateAsync(contactDbOwner);
                }

                // gerar notificação
                var notification = new Notification
                {
                    Name        = "Você tem uma solicitação de contado!",
                    Description = "Entre em seu perfil e verifique os contatos!",
                    CreatedAt   = Util.CurrentDateTime()
                };

                await _notificationMananger.CreateAsync(notification);

                var personNotification = new NotificationPerson
                {
                    Notification = notification,
                    Person       = libraryBook.Library.Person,
                    Active       = true,
                    CreatedAt    = Util.CurrentDateTime()
                };

                await _notificationPersonMananger
                .CreateAsync(personNotification);

                _uow.Commit();

                return(Created(
                           new Uri(Url.ActionLink("Create", "Contact")), new ResponseViewModel
                {
                    Result = null,
                    Status = Constants.Sucess
                }));
            }
            catch (Exception ex)
            {
                // gerar log
                return(BadRequest(new ResponseErrorViewModel
                {
                    Status = Constants.Error,
                    Errors = new List <object> {
                        Util.ReturnException(ex)
                    }
                }));
            }
        }