Skip to content

Commit

Permalink
сделал профиль админа и пофиксил баг с request
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkPolyakov committed Dec 19, 2024
1 parent b091e2f commit 369deed
Show file tree
Hide file tree
Showing 24 changed files with 672 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Program/backend/Controllers/ProfileController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public ProfileController(IProfileService profileService)
this.profileService = profileService;
}

[Authorize(Roles = "Client")]
[Authorize(Roles = "Client,Admin")]
[HttpGet("GetUserProfile")]
public async Task<ActionResult<UserModel>> GetUserProfile(int userId)
{
Expand Down
38 changes: 38 additions & 0 deletions Program/backend/Controllers/RequestController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using backend.Interfaces.RequestInterfaces;
using backend.Models;
using backend.Models.DTO.Request;
using backend.Models.DTO.RequestHelp;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

Expand Down Expand Up @@ -51,5 +52,42 @@ public async Task<ActionResult<IEnumerable<Request>>> GetUsersRequests(int userI
return Conflict(ex.Message);
}
}

[Authorize(Roles = "Admin")]
[HttpPost("EditRequestStatus")]
public async Task<IActionResult> EditRequestStatus([FromBody] EditRequestDTO requestDTO )
{
try
{
await requestService.EditRequestStatusService(requestDTO.RequestId, requestDTO.RequestStatus);

if(requestDTO.RequestStatus == Enums.RequestStatus.Ready)
{
return Ok("Заявка успешно принята");
}

return Ok("Заявка успешно отклонена");
}
catch (Exception ex)
{
return Conflict(ex.Message);
}
}

[Authorize(Roles = "Admin")]
[HttpGet("GetAllWaitingRequests")]
public async Task<ActionResult<IEnumerable<Request>>> GetAllWaitingRequests()
{
try
{
var requests = await requestService.GetAllWaitingRequestsService();

return Ok(requests);
}
catch (Exception ex)
{
return Conflict(ex.Message);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using backend.Enums;
using backend.Models;

namespace backend.Interfaces.RequestInterfaces
Expand All @@ -10,5 +11,7 @@ public interface IRequestRepository
{
Task<IEnumerable<Request>> GetUsersRequestsAtDB(int userId);
Task CreateRequestAtDB(Request request);
Task EditRequestStatusAtDB(int requestId, RequestStatus status);
Task<IEnumerable<Request>> GetAllWaitingRequestsFromDB();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using backend.Enums;
using backend.Models;
using backend.Models.DTO.Request;

Expand All @@ -11,5 +12,7 @@ public interface IRequestService
{
Task<IEnumerable<Request>> GetUsersRequests(int userId);
Task CreateRequest(RequestDTO requestDTO);
Task EditRequestStatusService(int requestId, RequestStatus status);
Task<IEnumerable<Request>> GetAllWaitingRequestsService();
}
}
14 changes: 14 additions & 0 deletions Program/backend/Models/DTO/RequestHelp/EditRequestDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using backend.Enums;

namespace backend.Models.DTO.RequestHelp
{
public class EditRequestDTO
{
public int RequestId {get; set;}
public RequestStatus RequestStatus {get; set;}
}
}
2 changes: 1 addition & 1 deletion Program/backend/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@
app.UseSwaggerUI();
}

app.UseCors("CorsPolicy");
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthentication(); // Аутентификация
app.UseAuthorization(); // Авторизация
app.UseCors("CorsPolicy");

// Configure endpoints for controllers
app.UseEndpoints(endpoints =>
Expand Down
24 changes: 24 additions & 0 deletions Program/backend/Repositories/RequestRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Threading.Tasks;
using backend.Data;
using backend.Enums;
using backend.Interfaces.RequestInterfaces;
using backend.Models;
using Microsoft.EntityFrameworkCore;
Expand All @@ -28,6 +29,29 @@ public async Task CreateRequestAtDB(Request request)
await Save();
}

public async Task EditRequestStatusAtDB(int requestId, RequestStatus status)
{
var request = await _context.Requests
.FirstOrDefaultAsync(r => r.Id == requestId);

if (request == null)
{
throw new Exception("Заявка не найдена в бд");
}

request.RequestStatus = status;
await Save();
}

public async Task<IEnumerable<Request>> GetAllWaitingRequestsFromDB()
{
var requests = await _context.Requests
.Where(r => r.RequestStatus == Enums.RequestStatus.InProcess)
.ToListAsync();

return requests;
}

public async Task<IEnumerable<Request>> GetUsersRequestsAtDB(int userId)
{
var checkUserId = await IsUserInDataBase(userId);
Expand Down
18 changes: 18 additions & 0 deletions Program/backend/Services/RequestService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using backend.Enums;
using backend.Interfaces.RequestInterfaces;
using backend.Models;
using backend.Models.DTO.Request;
Expand All @@ -17,6 +18,16 @@ public RequestService(IRequestRepository requestRepository)
this.requestRepository = requestRepository;
}

public async Task EditRequestStatusService(int requestId, RequestStatus requestStatus)
{
if (requestId < 0)
{
throw new Exception("requestId не может быть отрицательным");
}

await requestRepository.EditRequestStatusAtDB(requestId, requestStatus);
}

public async Task CreateRequest(RequestDTO requestDTO)
{
if (requestDTO.UserId < 0)
Expand All @@ -34,6 +45,13 @@ public async Task CreateRequest(RequestDTO requestDTO)
await requestRepository.CreateRequestAtDB(newRequest);
}

public async Task<IEnumerable<Request>> GetAllWaitingRequestsService()
{
var requests = await requestRepository.GetAllWaitingRequestsFromDB();

return requests;
}

public async Task<IEnumerable<Request>> GetUsersRequests(int userId)
{
if (userId < 0)
Expand Down
Binary file modified Program/backend/bin/Debug/net9.0/backend.dll
Binary file not shown.
Binary file modified Program/backend/bin/Debug/net9.0/backend.pdb
Binary file not shown.
2 changes: 1 addition & 1 deletion Program/backend/obj/Debug/net9.0/backend.AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
[assembly: System.Reflection.AssemblyCompanyAttribute("backend")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+5eaabc4feca8b485b2f115cc0ecc858126022dc9")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+b091e2f75a169e5cf887f2e80fabb1ee3da9876e")]
[assembly: System.Reflection.AssemblyProductAttribute("backend")]
[assembly: System.Reflection.AssemblyTitleAttribute("backend")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2120636d2c07a26b7799f604bb4f380cdf3cfb49f8ed4c6e0f27f6a7b0cdc1ac
af513f5a2d59734c7491c557bfb23793e894d03bf1a746c8772b8b68f82281b9
Original file line number Diff line number Diff line change
@@ -1 +1 @@
48d24789e60b976fab311e58f5a0c166afe297cdaa3d94fd58191c00f42001fe
238b8273d4a4df02ceaf5c22895c67ffb857725d1d45a08ac8e63f834c7af515
Binary file modified Program/backend/obj/Debug/net9.0/backend.dll
Binary file not shown.
Binary file modified Program/backend/obj/Debug/net9.0/backend.pdb
Binary file not shown.
2 changes: 1 addition & 1 deletion Program/backend/obj/Debug/net9.0/backend.sourcelink.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"documents":{"/Users/markpolakov/Desktop/WebApi_Insurance/*":"https://raw.githubusercontent.com/babidjon666/WebApi_Insurance/5eaabc4feca8b485b2f115cc0ecc858126022dc9/*"}}
{"documents":{"/Users/markpolakov/Desktop/WebApi_Insurance/*":"https://raw.githubusercontent.com/babidjon666/WebApi_Insurance/b091e2f75a169e5cf887f2e80fabb1ee3da9876e/*"}}
Binary file modified Program/backend/obj/Debug/net9.0/ref/backend.dll
Binary file not shown.
Binary file modified Program/backend/obj/Debug/net9.0/refint/backend.dll
Binary file not shown.
97 changes: 93 additions & 4 deletions Program/frontend/src/Components/Profiles/Admin/AdminProfile.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,98 @@
import React from "react";
import React, { useState, useEffect } from 'react';
import { useParams, useNavigate } from 'react-router-dom';
import { Avatar, Menu, Modal} from 'antd';

import { getProfile } from "../Client/ClientService";

import { AdminTerms } from './AdminTerms';
import { AdminRequests } from './AdminRequests';

export const AdminProfile = () =>{
return(
<div>
Профиль админа
const [user, setUser] = useState(null);
const { id } = useParams();
const [activeSection, setActiveSection] = useState('Terms');
const navigate = useNavigate();

useEffect(() => {
fetchProfile();
}, []);

const fetchProfile = async () =>{
try {
const newUser = await getProfile(id);
setUser(newUser);
} catch (error) {
console.error('Ошибка при получении профиля:', error);
}
}

const handleSectionChange = (section) => {
setActiveSection(section);
};

const handleLogout = () => {
Modal.confirm({
title: 'Exit',
content: 'Are you sure you want to leave the page?',
okText: 'Yes',
cancelText: 'No',
onOk: () => {
navigate(`/login`, { replace: true });
},
onCancel: () => {
handleSectionChange("personalInfo");
},
});
};

if (!user){
return<div>Loading...</div>
}

return (
<div className="dashboard-container">
<div className="profile-header">
<Avatar
src={`https://api.dicebear.com/7.x/miniavs/svg?seed=${id}`}
size={100}
style={{
marginRight: '16px',
border: '2px solid #f0f0f0',
boxShadow: '0 4px 15px rgba(0, 0, 0, 0.1)',
transition: 'transform 0.3s ease',
}}
className="profile-avatar"
/>
<div className="profile-info">
<h2 className="profile-title">Admin Profile</h2>
<div className="profile-details">
<h3 className="profile-name">Name: {user.name}</h3>
<h3 className="profile-surname">Surname: {user.surname} </h3>
</div>
</div>
</div>

<Menu
onClick={({ key }) => handleSectionChange(key)}
selectedKeys={[activeSection]}
mode="horizontal"
style={{ marginBottom: '20px' }}
>
<Menu.Item key="Terms">Terms</Menu.Item>
<Menu.Item key="Requests">Requests</Menu.Item>
<Menu.Item key="logout" onClick={handleLogout}>
Logout
</Menu.Item>
</Menu>

<div className="dashboard-content">
{activeSection === 'Terms' && user &&(
<AdminTerms />
)}
{activeSection === 'Requests' && user &&(
<AdminRequests />
)}
</div>
</div>
);
};
Loading

0 comments on commit 369deed

Please sign in to comment.